Search code examples
asp.net.nethttprequesthttpcontext

How HttpContext works in .NET


I am trying to understand the detail of how httpcontext works in .NET ?

What I couldnt understand is the class of HttpContext has a field of static HttpContext object. So for each request from client creates an instance of an httpcontext or not ? Why it defined as static ? Does it mean one application can only has one httpcontext ?

I am confused so the way that ask the question can be little bit complex. I hope it is understandable.

Thanks in advance,


Solution

  • When you access the static member, it resolves the current request relative to the thread.

    No, this does not mean there is only one, in the same way that Thread.Current doesnt mean there is only one thread. Each request has a different HttpContext.

    As a trivial way to do something similar (I don't know if it is implemented this way, though);

    [ThreadStatic]
    public static string TryMeFromDifferentThreads;