我正在尝试在HttpModule中访问一个页面,我认为应该通过调用HttpContext.Current.Handler来实现这一点(这应该引用当前页面),但是我一直得到null。
我正在使用.Net 3.5框架进行开发。
我正在AuthorizeRequest和AuthenticateRequest事件上检查此问题。
谢谢。
我正在使用.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
。你需要在 Init
方法的参数中注册事件处理程序,并在那里完成你的工作。