为什么HttpContext.Current.Handler为空?
-
20-08-2019 - |
题
我试图内的HttpModule,我想我应该调用HttpContext.Current.Handler(这应该参考当前页)这样做,但我得到空所有的时间。
访问一个页面我使用的.Net框架3.5发展。
我检查此上的AuthorizeRequest和AuthenticateRequest
感谢。
解决方案
可能,该请求没有被发放到一个处理程序,但(例如,你在BeginRequest
)。
其他提示
在AuthorizeRequest
和AuthenticateRequest
,该处理程序尚未创建。 (如果该请求被拒绝不应该被创建的处理程序)。因此,这个属性是空值。
为什么你的Page
,什么是你想怎么办?
您可以尝试处理PostMapRequestHandler
,发生这解决了Page
后,并抛出HttpException
或调用Response.End
如果你决定拒绝该请求。
但是,请注意,以获得处理程序的实例,它的构造必须运行;确保它不会做任何事情的关键或敏感。
我有类似的问题,终于找到了解决方案。 我的问题是返回null然后使用外部类的代码。 我很抱歉,我的英语不是很好。
通过代码溶液:(已经测试)结果 通过测试:VS 2010
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
//[Description of MyNamespace]
//|================================================================================>
//|-----*(In The Name Of GOD)*-----
//|================================================================================>
namespace MyNamespace
{
//Most Be "partial class" And ": System.Web.UI.Page" !!!!
public partial class MyClass : System.Web.UI.Page
{
//|============================================================>
//| Value Of Class.
//|============================================================>
static System.Web.UI.Page Page1 = null;
static System.Web.UI.Page Page2 = null;
int form1Index = -0;
//Most Be Static Method!!!!
public static void GetMyPage()
{
//Both are a result code.
//هر دو کد یه نتیجه می دهد
Page1 = HttpContext.Current.Handler as System.Web.UI.Page;
Page2 = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler;
}
//|============================================================>
//| DO() Methods Of MyClass Class.
//|============================================================>
public void DO()
{
//Call Your Static Method => GetMyPage()
GetMyPage();
if (Page1 != null)
{
for (int i = 0; i < Page1.Controls.Count; i++)
{
if (Page1.Controls[i].ID == "form1")
{
form1Index = i;
break;
}
}
}
if (form1Index != -0)
{
for (int j = 0; j < Page1.Controls[form1Index].Controls.Count; j++)
{
string ControlsID = Page1.Controls[form1Index].Controls[j].ID;
// Code location ...
//محل قرار گیری کد ها...
}
}
}
//|============================================================>
//| Destructor Methods MyClass Class.
//|============================================================>
~MyClass() { }
}
}
在用什么方法,你访问这个属性?
在IHttpModule.Init
,它将被null
。您需要在收到作为参数传递给application
方法Init
注册事件处理程序,并做你的工作在那里。
不隶属于 StackOverflow