我正在尝试访问 HttpModule 中的页面,我认为我应该通过调用 HttpContext.Current.Handler 来做到这一点(这应该引用当前页面),但我一直在获取 null。
我正在使用 .Net 3.5 框架进行开发。
我正在检查 AuthorizeRequest 和 AuthenticateRequest
谢谢。
我正在尝试访问 HttpModule 中的页面,我认为我应该通过调用 HttpContext.Current.Handler 来做到这一点(这应该引用当前页面),但我一直在获取 null。
我正在使用 .Net 3.5 框架进行开发。
我正在检查 AuthorizeRequest 和 AuthenticateRequest
谢谢。
可能,该请求尚未分发给处理程序(例如,您在BeginRequest
)。
在AuthorizeRequest
andAuthenticateRequest
中,处理程序尚未创建。(如果请求被拒绝,则不应创建处理程序)因此,此属性为空。
你为什么这样做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
方法的参数并在那里完成您的工作。