frenchie
frenchie

Reputation: 51937

http module: Request is not available

I'm creating an http module where I want to check if a request is coming from an authenticated user and redirect to the login page if it's not.

I registered the module in the web.config file and I have the following code that's throwing an exception:

public class IsAuthModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication TheApp)
    {
        var TheRequest = TheApp.Request;

    }
}

It throwing an exception that says "Request is not available in this context"

What am I doing wrong?

Upvotes: 4

Views: 1665

Answers (1)

onof
onof

Reputation: 17367

In the Init stage you have no request in progress. You have to subscribe the event for beginning of a request:

public void Init(HttpApplication TheApp)
{
    TheApp.BeginRequest += Application_BeginRequest;

    // End Request handler
    //application.EndRequest += Application_EndRequest;
}

private void Application_BeginRequest(Object source, EventArgs e) 
{
  // do something
}

Upvotes: 6

Related Questions