03Usr
03Usr

Reputation: 3435

Where to implement Global.asax methods

I am working on an ASP.Net application and currently the Global.asax contains the usual 5 methods:

  1. Application_Start
  2. Application_End
  3. Session_Start
  4. Session_End
  5. Application_Error

However, I needed to implement the Application_AuthenticateRequest method as well, which is not a problem, I have just added it in Global.asax but in an another application I have seen this method being implemented elsewhere in another class which implements the IHttpModule interface.

How is this possible? The same app does not have the Application_AuthenticateRequest in Global.asax, their Global.asax looks like this:

void Application_BeginRequest(object sender, EventArgs e)
{
    myConfig.Init();
}

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    myConfig.Init();
    if (InstallerHelper.ConnectionStringIsSet())
    {
        //initialize IoC
        IoC.InitializeWith(new DependencyResolverFactory());

        //initialize task manager
        TaskManager.Instance.Initialize(NopConfig.ScheduleTasks);
        TaskManager.Instance.Start();
    }
}

void Application_End(object sender, EventArgs e)
{
    //  Code that runs on application shutdown
    if (InstallerHelper.ConnectionStringIsSet())
    {
        TaskManager.Instance.Stop();
    }
}

What makes the Application_AuthenticateRequest method run?

Upvotes: 0

Views: 8560

Answers (2)

03Usr
03Usr

Reputation: 3435

Basically the example I have been looking at created their own HTTP module and registered it in the web.config file:

They have created a new HTTP module like this:

public class MembershipHttpModule : IHttpModule
{
    public void Application_AuthenticateRequest(object sender, EventArgs e)
    {
        // Fires upon attempting to authenticate the user
        ...
    }

    public void Dispose()
    {
    }

    public void Init(HttpApplication application)
    {
        application.AuthenticateRequest += new EventHandler(this.Application_AuthenticateRequest);
    }
}

also added the below to the web.config file:

<httpModules>
  <add name="MembershipHttpModule" type="MembershipHttpModule, App_Code"/>
</httpModules>   

As explained in the @Darin Dimitrov's link above: Modules must be registered to receive notifications from the request pipeline. The most common way to register an HTTP module is in the application's Web.config file. In IIS 7.0, the unified request pipeline also enables you to register a module in other ways, which includes through IIS Manager and through the Appcmd.exe command-line tool.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

I would first recommend you read about HTTP handlers and modules in ASP.NET. Then you will know that in an ASP.NET application you could have multiple modules registered which will run for every request and you have the possibility to subscribe to different events of the request lifecycle, the same way you could do it in Global.asax. The advantage of this approach is that you could put the modules into a reusable assembly that you use in multiple applications and which avoids you the need to repeat the same code over and over again.

Upvotes: 2

Related Questions