ASP.NET Core is a cross-platform, open source framework for developing Web applications on Windows, Mac, and Linux operating systems (OS). You can develop ASP.NET Core applications using any of the following ides:
- Visual Studio
- Visual Studio for Mac
- Visual Studio Code
In this blog post, we’ll learn how to migrate ASP.NET IHttpHandler and IHttpModule to ASP.NET Core middleware and provide code examples.
Let’s get started!
ASP.NET IHttpHandler
In an ASP.NET application, an HTTP handler is a process that executes on each response to the Web server. We can create our own custom HTTP handlers.
Below is the code to redirect all.aspx pages to a new page.
public class RedirectionHandler : IHttpHandler
{
public bool IsReusable
{
get { return false; }
}
public void ProcessRequest(HttpContext context)
{
var response = context.Response;
response.Write("<p>Process files with .aspx extension</p>");
// Any redirection logic can be written here.
}
}
Copy the code
Add the following code to web.config:
<add name="RedirectionHandler" verb="*" path="*.aspx" type="MyWebApplication.RedirectionHandler" resourceType="Unspecified"/>
Copy the code
ASP.NET IHTTPModule
The IHttpModule will also be executed before and after the HTTP handler for each request of the application. They help us validate incoming and outgoing requests and modify them.
Here is the IHttpModule code to restrict users based on their IP addresses.
public class IPRestrictionModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += (source, arguments) => { var application = (HttpApplication)source; var beginContext = application.Context; beginContext.Response.Write("<p>Restrict Users based on IP</p>"); // Code logic comes here. }; context.EndRequest += (source, arguments) => { var application = (HttpApplication)source; var endContext = application.Context; endContext.Response.Write("<p>Request ended.</p>"); }; }}Copy the code
Add the following code to web.config:
<add name="RestrictionModule" type=" MyWebApplication.IPRestrictionModule" />
Copy the code
ASP.NET Core Middleware
In ASP.NET Core applications, the middleware component replaces the IHttpHandler and IHttpModule. It is the component that executes for each request. We can use the IApplicationBuilder interface to add middleware to the Configure method of the Startup class.
The following four methods can be used:
Run
Terminate the HTTP pipe.
Use
Add middleware to the request pipeline.
Map
Match the request delegate based on the request path
MapWhen
Support for predicate-based middleware branching.
Let’s see how to migrate ASP.NET IHttpHandler and IHttpModule to ASP.NET Core middleware!
Migrate IHttpHandler to ASP.NET Core middleware
1. Use the following code creates RedirectionHandlerMiddleware class
public class RedirectionHandlerMiddleware
{
private RequestDelegate _next;
public RedirectionHandlerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
await context.Response.WriteAsync("<p>Process files with .aspx extension</p>");
// Any Redirection logic can be return here.
}
}
Copy the code
2. Create an extension methods in ApplicationBuilder, used in pipeline RedirectionHandlerMiddleware on request.
3. Then create a class called MiddlewareExtension for the extension method and use the following code in it.
public static class MiddlewareExtension { public static IApplicationBuilder UseRedirectionHanlderMiddleware (this IApplicationBuilder applicationBuilder) { return applicationBuilder.UseMiddleware<RedirectionHandlerMiddleware>(); }}Copy the code
4. We need to include the next code in the startup. cs file.
app.MapWhen(context => context.Request.Path.ToString().EndsWith(".aspx"),
appBuilder => {
appBuilder.UseRedirectionHanlderMiddleware();
});
Copy the code
We have now completed the migration of IHttpHandler.
Migrate IHttpModule to ASP.NET Core middleware
1. Use the following code creates IPRestrictionModuleMiddleware class.
public class IPRestrictionModuleMiddleware { private RequestDelegate _next; public IPRestrictionModuleMiddleware (RequestDelegate next) { _next = next; } public async Task Invoke(HttpContext context) { await context.Response.WriteAsync("<p>Begin request</p>"); await _next.Invoke(context); await context.Response.WriteAsync("<p>End request</p>"); }}Copy the code
2. As before, we need to add an extension method to add middleware in the request pipeline.
3. Then add the following code to the existing MiddlewareExtension class:
public static class MiddlewareExtensions { public static IApplicationBuilder UseRedirectionHanlderMiddleware (this IApplicationBuilder applicationBuilder) { return applicationBuilder.UseMiddleware<RedirectionHandlerMiddleware>(); } public static IApplicationBuilder UseIPRestrictionModuleMiddleware (this IApplicationBuilder builder) { return builder.UseMiddleware<IPRestrictionModuleMiddleware>(); }}Copy the code
4. Then include the middleware in the startup. cs file.
// For Module
app.UseIPRestrictionModuleMiddleware();
// For Handler
app.MapWhen(context => context.Request.Path.ToString().EndsWith(".aspx"),
appBuilder => {
appBuilder.UseRedirectionHanlderMiddleware();
});
Copy the code
This completes the migration of the IHttpModule.