Unique way to identify page instance within HttpContext

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?

Answers


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;
        }

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.


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.


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.


Need Your Help

Postgres ltree in hibernate query

hibernate postgresql playframework-1.x ltree

I use Play Framework 1.2.5.3 and PostgreSQL 9.x.

Can you/How do you save CPU and memory by choosing wisely

java data-structures cpu-usage cpu-speed

I understand the JVM optimizes some things for you (not clear on which things yet), but lets say I were to do this: