Search code examples
asp.nethttpmodulehttpcontext

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?


Solution

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