之间HttpContext.Request和要求的不同 [英] Difference between HttpContext.Request and Request
问题描述
什么是三ASP.NET对象之间的差异:
- HttpContext.Current.Request
- HttpContext.Request
- 请求
这些是完全一样的?
修改1
做这些对象的行为不同在的Global.asax
/ global.asax.vb
和 Default.aspx的
/ default.aspx.vb
。
修改2
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然后
'做一点事
结束如果
结束小组
好了:
- <一个href="http://msdn.microsoft.com/en-us/library/system.web.httpcontext.current.aspx"><$c$c>HttpContext.Current$c$c>是的静态的属性返回当前
的HttpContext
的线程 - <一个href="http://msdn.microsoft.com/en-us/library/system.web.httpcontext.request.aspx"><$c$c>HttpContext.Request$c$c>是一种的实例的属性返回
的Htt prequest
为的HttpContext
你叫它在 - <一个href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.request.aspx"><$c$c>Page.Request$c$c>在
第一个实例属性
,返回要求
与调用它(通常隐含<$ C页面关联$ C>这)
所以 HttpContext.Current.Request
将,以获得与当前线程相关的要求同时使用前两个属性。如果你在处理请求的线程,这将是一样的 Page.Request
这所呈现的相关页面中。
不过,如果你的渲染揭开序幕的不同的的线程,在其他线程的code运行仍然可以在要求
通过 Page.Request
(因为它只是一个正常的财产),但将没有的HttpContext
与线程关联 - 所以 HttpContext.Current.Request
是行不通的。
编辑:为了应对已编辑的问题,在Global.asax中的要求
属性是指<一个href="http://msdn.microsoft.com/en-us/library/system.web.httpapplication.request.aspx"><$c$c>HttpApplication.Request$c$c>,并可能是使用的最佳方法。 HttpContext.Request
将无法正常工作,因为这是试图访问一个静态属性,就好像它是一个实例属性。 HttpContext.Current.Request
的应的工作,假设情况下已经与该点线程关联。
What is the difference between the three ASP.NET objects:
- HttpContext.Current.Request
- HttpContext.Request
- Request
Are these exactly the same?
Edit 1
Do these objects behave differently inside global.asax
/global.asax.vb
and default.aspx
/default.aspx.vb
.
Edit 2
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 currentHttpContext
for the threadHttpContext.Request
is an instance property returning theHttpRequest
for theHttpContext
you call it onPage.Request
is an instance property inPage
, returning theRequest
associated with the page you call it on (typically implicitlythis
)
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.
这篇关于之间HttpContext.Request和要求的不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!