M
meenakshiBalekar
Introduction
In the ever-evolving landscape of web development, managing and optimizing the flow of incoming and outgoing traffic is crucial. Internet Information Services (IIS) provides a robust platform for web applications, and one of its powerful features is the ability to use handlers.
Custom handlers allow developers to create tailored solutions that enhance the functionality and performance of their applications. In this blog, we will explore what handlers are in IIS, how to create custom handlers in an ASP.NET framework application, and the advantages of using them.
Read more about this on my previous blog :
IIS Handlers
If you want to do the same using modules, you can read more on below :
Remove unwanted HTTP headers using IIS Modules
What are Handlers in IIS
Handlers in IIS are components responsible for processing requests made to the server. When a client sends a request, IIS determines the appropriate handler to process that request based on its type. Handlers work at a lower level than modules, directly interacting with the HTTP request and response. They can control the entire request pipeline, making them essential for tasks that require specific handling of HTTP requests, such as file downloads, dynamic content generation, and custom authentication mechanisms.
How to Create Custom Handler in ASP.NET Framework Application
Creating a custom handler in an ASP.NET framework application involves several steps:
I am making use of .NET framework version 4.8.1 for creating this class library on Visual studio 2022
Step 1: Create a new Visual Studio Project
Name it appropriately, e.g., " MyCustomHandler ".
Step 2: Implement the IHttpHandler Interface
To create a custom handler, you need to implement the `IHttpHandler` interface, which requires defining two methods: `ProcessRequest` and `IsReusable`.
Code:
namespace RemoveHeadersUsingHandlers
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class RemoveHeadersMiddleware
{
private readonly RequestDelegate _next;
public RemoveHeadersMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
context.Response.OnStarting(() =>
{
// List of headers to remove
var headersToRemove = new[] { "X-AspNetMvc-Version", "Server", "Content-Type" };
foreach (var header in headersToRemove)
{
context.Response.Headers.Remove(header);
}
context.Response.Headers.Add("X-Frame-Options","SAMEORIGIN");
context.Response.Headers.Add("X-Powered-By", "123.123.123.123");
context.Response.Headers.Add("X-Content-Type", "nosniff");
return Task.CompletedTask;
});
// Call the next delegate/middleware in the pipeline
await _next(context);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class RemoveHeadersMiddlewareExtensions
{
public static IApplicationBuilder UseRemoveHeadersMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<RemoveHeadersMiddleware>();
}
}
Step 3A: Register the Handler in Web.config
After creating the handler class, you need to register it in the `Web.config` file of your application.
Code:
<handlers>
<add name=" MyCustomHandler " path="C:\inetpub\URLRewrite\bin\CustomHandler.dll" verb="*" type=" MyCustomHandler. MyCustomHandler, MyCustomHandler "/>
</handlers>
Step 3B: Register the Handler Via IIS UI
After creating the handler class, you can place the ‘DLL’ in the bin folder of your application.
Then go to the ‘Handler Mapping’
and click on ‘Add Managed Handler’
Enter the details of the handler
This is how you can add the Custom handler to your application hosted on IIS
Step 3: Access the Custom Handler
Once registered, you can access your custom handler through the specified path. In this example, navigating to `` will trigger the custom handler a...iis-for-asp/ba-p/4252535"]Continue reading...