什么是三ASP.NET对象之间的差异:
HttpContext.Current.Request HttpContext.Request 请求这些是完全一样的?
做这些对象的行为不同在的Global.asax
/ global.asax.vb
和 Default.aspx的
/ default.aspx.vb
。
OK,我会尽量具体的这个时候。以下哪一项我应该使用:
文件:Global.asax中
保护小组的Application_BeginRequest(BYVAL发件人为对象,BYVALË作为System.EventArgs)
如果Request.Url.Port = 80然后
如果HttpContext.Current.Request.Url.Port = 80然后
如果HttpContext.Request.Url.Port = 80然后
'做一点事
结束如果
结束小组
文件:Default.aspx的
公共小组的Page_Load(BYVAL发件人为对象,BYVALË作为System.EventArgs)
如果Request.Url.Port = 80然后
如果HttpContext.Current.Request.Url.Port = 80然后
如果HttpContext.Request.Url.Port = 80然后
'做一点事
结束如果
结束小组
解决方案
好了:
HttpContext.Current是的静态的属性返回当前的HttpContext
的线程
HttpContext.Request是一种的实例的属性返回的Htt prequest
为的HttpContext
你叫它在
Page.Request在第一个实例属性
,返回要求
与调用它(通常隐含这)
所以 HttpContext.Current.Request
将,以获得与当前线程相关的要求同时使用前两个属性。如果你在处理请求的线程,这将是一样的 Page.Request
这所呈现的相关页面中。
不过,如果你的渲染揭开序幕的不同的的线程,在其他线程的code运行仍然可以在要求
通过 Page.Request
(因为它只是一个正常的财产),但将没有的HttpContext
与线程关联 - 所以 HttpContext.Current.Request
是行不通的。
编辑:为了应对已编辑的问题,在Global.asax中的要求
属性是指HttpApplication.Request,并可能是使用的最佳方法。 HttpContext.Request
将无法正常工作,因为这是试图访问一个静态属性,就好像它是一个实例属性。 HttpContext.Current.Request
的应的工作,假设情况下已经与该点线程关联。
What is the difference between the three ASP.NET objects:
HttpContext.Current.Request HttpContext.Request RequestAre 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
解决方案
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.