Register handlers for application events in ASP.NET 5

If I wanted to handle application events in my ASP.NET application, I would register a handler in my Global.asax :

 protected void Application_BeginRequest(object sender, EventArgs e) { ... } 

Global.asax been removed from ASP.NET 5. How do I handle such events now?

+5
source share
2 answers

The way to run some logic for each request in ASP.NET 5 is through Middlewares. Here is an example of middleware:

 public class FooMiddleware { private readonly RequestDelegate _next; public FooMiddleware(RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { // this will run per each request // do your stuff and call next middleware inside the chain. return _next.Invoke(context); } } 

You can then register this in your Startup class:

 public class Startup { public void Configure(IApplicationBuilder app) { app.UseMiddleware<FooMiddleware>(); } } 

See here for more information on Middlewares in ASP.NET 5 .

For any entry-level launch invocation calls, see the application launch documentation .

+2
source

ASP.NET applications can live without global.asax.

HTTPModule is an alternative to global.asax.

More details here .

-1
source

All Articles