What is the difference between the three ASP.NET objects:
Are these exactly the same?
Do these objects behave differently inside global.asax
/global.asax.vb
and default.aspx
/default.aspx.vb
.
OK I'll try to be specific this time. Which of the following should I use:
' File: global.asax
Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)
If Request.Url.Port = 80 Then
'If HttpContext.Current.Request.Url.Port = 80 Then
'If HttpContext.Request.Url.Port = 80 Then
'do something
End If
End Sub
' File: default.aspx
Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If Request.Url.Port = 80 Then
'If HttpContext.Current.Request.Url.Port = 80 Then
'If HttpContext.Request.Url.Port = 80 Then
'do something
End If
End Sub
HTTPContext. Current is a static property. This property is a static property of the HttpContext class. The property stores the HttpContext instance that applies to the current request. The properties of this instance are the non-static properties of the HttpContext class.
HttpRequest is a subset of HttpContext . In other words, the HttpContext includes the response, the request, and various other data that's not relevant to a specific request or response; such as the web application, cached data, server settings and variables, session state, the authenticated user, etc.
HttpContext isn't thread-safe. Reading or writing properties of the HttpContext outside of processing a request can result in a NullReferenceException.
Well:
HttpContext.Current
is a static property returning the current HttpContext
for the threadHttpContext.Request
is an instance property returning the HttpRequest
for the HttpContext
you call it onPage.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.
They are all the same. There are simply various shortcuts built-into classes you're inheriting from such as Controller and Page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With