Difference between HttpContext.Request and Request

https://stackoverflow.com/questions/5547989/difference-between-httpcontext-request-and-request

Well:

  • HttpContext.Current is a static property returning the current HttpContext for the thread
  • HttpContext.Request is an instance property returning the HttpRequest for the HttpContext you call it on
  • Page.Request is an instance property in Page, returning the Request associated with the page you call it on (typically implicitly this)

So HttpContext.Current.Request will use both of the first two properties in order to get the request associated with the current thread. If you're in the thread dealing with a request, that's going to be the same as the Page.Request within the relevant page which is being rendered.

However, if your rendering kicks off a different thread, the code running in the other thread can still get at the Request via Page.Request (because it's just a normal property) but there'll be no HttpContext associated with the thread - so HttpContext.Current.Request wouldn't work.

EDIT: To respond to the edited question, in global.asax the Request property refers to HttpApplication.Request, and is probably the best approach to use. HttpContext.Request won't work, because that's trying to access a static property as if it were an instance property. HttpContext.Current.Request should work, assuming the context has been associated with the thread by that point.

 

posted @ 2018-09-26 14:55  ChuckLu  阅读(374)  评论(0编辑  收藏  举报