views:

54

answers:

4

You can get the name of a page within HttpContext via Request.Path.

Is there a way to distinguish between different requests from the same page?

That is when two different instances of yourpage.aspx make a request, how can you distinguish between the two using HttpContext?

+1  A: 

Nothing built into ASP.NET will allow you to differentiate different "page instances" or requests from them.

However, you can easily add a Guid to your view state to uniquely identify each page. This mechanism works fine when you are in the Page class itself. If you need to identify requests before you reach the page handler, you need to use a different mechanism (since view state is not yet restored).

The Page.LoadComplete event is a reasonable place to check if a Guid is associated with the page, and if not, create one.

LBushkin
adding a guid to the viewstate will only identify a unique browser not a unique page instance. I think however, you may have the answer he's looking for - just not the question he asked.
matt-dot-net
+1  A: 

If you're using authentication, would it work for you to distinguish which user submitted the page?

You could use System.Web.Httpcontext.Current.User.Identity.Name.

DOK
+1  A: 

you probably want to do this in a base Page class, but here's what i would do

public partial class Default : System.Web.UI.Page
{
    private Guid _instanceID;

    public Guid InstanceID
    {
        get { return _instanceID; }
    }

    /// <summary>
    /// Constructor
    /// </summary>
    public Default()
    {
        this._instanceID = Guid.NewGuid();
    }
}

then using the HttpContext somewhere else in your code...

        if (HttpContext.Current.CurrentHandler is Default)
        {
            ((Default)HttpContext.Current.CurrentHandler).InstanceID;
        }
matt-dot-net
Unfortunately, at the point of processing where I'm accessing currenthandler, it's null, since it's before the page gets instantiated.
Steve
umm... then what is it you are trying to identify??? from your original question: "when two different instances of yourpage.aspx make a request, how can you distinguish between the two using HttpContext?"
matt-dot-net
+1  A: 

just throwing this out there: NInject (and other DI containers) use a scoping mechanism based on the HttpContext.Current object itself, so depending on what you're trying to do, you could attempt to retrieve a state object from the DI container and go from there.

dave thieben