2009-06-28 94 views
5

我试图访问HttpModule中的一个页面,我想我应该通过调用HttpContext.Current.Handler(这应该引用当前页面)来做到这一点,但我一直都是空的。为什么HttpContext.Current.Handler为空?

我正在开发使用.net 3.5框架。

我正在查看这个AuthorizeRequest和AuthenticateRequest

谢谢。

回答

8

也许,请求尚未交给处理程序(例如,您在BeginRequest)。

+0

我检查上的AuthorizeRequest和AuthenticateRequest – Paleta 2009-06-28 03:07:44

+2

@Paleta::(已测试)
通过测试您不能检查它。处理程序将在请求被认证/授权后被选择。你可以在PostMapRequestHandler事件中检查它。 – 2009-06-28 03:19:08

0

用什么方法访问此属性?

IHttpModule.Init,它将是null。您需要在作为Init方法的参数收到的application上注册事件处理程序,并在那里完成您的工作。

4

AuthorizeRequestAuthenticateRequest中,处理程序尚未创建。 (如果请求被拒绝,则不应创建处理程序)因此,此属性为空。

为什么你Page,你想做什么?

如果您决定拒绝请求,您可以尝试处理PostMapRequestHandler,这会在解决Page之后发生,并抛出HttpException或致电Response.End

但是,请注意,要获取处理程序的实例,其构造函数必须运行;确保它没有做任何关键或敏感的事情。

1

我有类似的问题,并最终找到解决方案。 我的问题被返回null,然后在外部类上使用此代码。 我为我的英语道歉并不好。通过代码

解决方案:2010 VS

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() { } 
} 

}