8

I'm trying to access a Page within an HttpModule and I think I should do this by calling HttpContext.Current.Handler (This should reference the current page) but I'm getting null all the time.

I'm developing using .Net 3.5 framework.

I'm checking this on AuthorizeRequest and AuthenticateRequest

Thanks.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Paleta
  • 970
  • 2
  • 13
  • 27

4 Answers4

12

Probably, the request has not been handed out to a handler yet (for example, you're in BeginRequest).

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
  • I'm checking on AuthorizeRequest and AuthenticateRequest – Paleta Jun 28 '09 at 03:07
  • 3
    @Paleta: You can't check it there. Handler will be selected after request is authenticated/authorized. You can check it in `PostMapRequestHandler` event and later. – Mehrdad Afshari Jun 28 '09 at 03:19
5

In AuthorizeRequest and AuthenticateRequest, the handler has not been created yet. (A handler should not be created if the request is denied) Therefore, this property is null.

Why do you the Page, and what are you trying to do?

You could try handling PostMapRequestHandler, which occurs after it resolves the Page, and throwing an HttpException or calling Response.End if you decide to deny the request.

However, please note that to get an instance of the handler, its constructor must run; make sure it doesn't do anything critical or sensitive.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • In my case, I've made my own authorization attributes that I've stuck on my `Page` subclasses and I'd like to check them in `AuthorizeRequest` - but I can't. Oh well. – Dai Sep 13 '19 at 11:28
2

I have similar problems and finally found solution . my problem was returned null then use this code on external class . I apologize for my English is not good .

solution via code :(Have Tested)
Tested by: 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() { }
}

}

Amin Ghaderi
  • 1,006
  • 13
  • 22
0

In what method are you accessing this property?

In IHttpModule.Init, it will be null. You need to register event handlers on the application received as a parameter to the Init method and do your work there.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964