How to do it...

We'll create a new HTTP module, and track the starting/ending point of the request. We will also add information to the beginning of the response and end of the response.

  1. First, let's see the anatomy of an HTTP module:
public class MyHttpModule : IHttpModule 
{ 
  public void Dispose(){} 
 
  public void Init(HttpApplication context) 
  { 
    context.BeginRequest += (source, args) => 
    { 
      context.Response.Write("MyHttpModule BeginRequest"); 
    }; 
 
    context.EndRequest += (source, args) => 
    { 
      context.Response.Write("MyHttpModule EndRequest"); 
    }; 
  } 
} 

HttpModule has to be configured in the Web.config file as follows:

<system.webServer> 
    <modules> 
      <add name="myModule" type="MyApp.HttpModules.MyHttpModule"/> 
    </modules> 
</system.webServer>
  1. Now, let's ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.