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 will 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, the 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 of the.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.
}
}
Add the following code to web.config:
<add name="RedirectionHandler" verb="*" path="*.aspx" type="MyWebApplication.RedirectionHandler" resourceType="Unspecified"/>
ASP.NET IHTTPModule
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 used to restrict users based on their IP address.
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>"); }; }}
Add the following code to web.config:
<add name="RestrictionModule" type=" MyWebApplication.IPRestrictionModule" />
ASP.NET Core Middleware
In an ASP.NET Core application, the middleware component replaces IHttpHandler and IHttpModule. It is a component that is executed on a per-request basis. We can use the IApplicationBuilder interface to add middleware to the Configure method of the Startup class.
You can use the following four methods:
Run: Terminates the HTTP pipe.
Use: Add middleware to the request pipeline.
Map: Matches the request delegate according to 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
- 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.
}
}
- Create an extension methods in ApplicationBuilder to use RedirectionHandlerMiddleware in request pipeline.
- 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>(); }}
- We need to include the next code in the startup.cs file.
app.MapWhen(context => context.Request.Path.ToString().EndsWith(".aspx"),
appBuilder => {
appBuilder.UseRedirectionHanlderMiddleware();
});
We have now completed the migration of IHttpHandler.
Migrate IHttpModule to ASP.NET Core Middleware
- 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>"); }}
- As before, we need to add an extension method to add middleware to the request pipeline.
- 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>(); }}
- 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();
});
This completes the migration of IHttpModule.
Original link: https://www.red-gate.com/simp…