Creating an IIS Hosted Handler with C# for Special Requests

Creating an IIS Hosted Handler with C# for Special Requests

In this article, we will create an IIS hosted handler using C# to handle some special requests for our website. This hosted handler can perform various functions, such as preventing hotlinking and downloading of audio, video, and image resources on our site, as well as providing special services like webSocket services. We can write a hosted handler to filter and process requests for these resources from clients.

1. Writing an IIS Hosted Handler

1. Create a new solution

Since this is just a test project, we will create a new solution on the desktop, selecting a C# class library project. Choose .NET Framework version 4.5 or higher. We will name the new project HttpHandlerTest, which will be used later when generating the DLL and configuring the hosting in IIS.

Creating an IIS Hosted Handler with C# for Special Requests

2. Create a new class

After the project is created, the system automatically generates a class file named MyClass, with the class name also set to MyClass.

Creating an IIS Hosted Handler with C# for Special Requests

3. Create your own class

In the previous step, the system created a class for us. We can either use this class to implement our functionality directly or create a new one. To make it easier to see the results later, we will delete the system-generated class and create a new class file named okme, and the class name will automatically be set to okme.

Creating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special Requests

4. Implement the IHttpHandler interface

Since we are writing an IIS hosted program, we need to implement the IHttpHandler interface to handle basic web requests. To implement the interface, simply add a colon followed by the interface name after the class name, like this: :IHttpHandler. We also need to import the relevant namespaces, and at this point, we may encounter errors as shown in the image below:

Creating an IIS Hosted Handler with C# for Special Requests

5. Import references

The error above is due to missing references. We can resolve this by importing the relevant references.

Creating an IIS Hosted Handler with C# for Special Requests

Right-click to add references

Creating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special Requests

6. The final example program is as follows

Don’t forget to implement this public bool IsReusable { get { return false; } }Creating an IIS Hosted Handler with C# for Special Requests

using System;using System.Linq;using System.Net.WebSockets;using System.Web;namespace HttpHandlerTest{    public class okme: IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.Write("Hello World");        }                public bool IsReusable { get { return false; } }    }}

7. Compile the project to obtain the DLL

Creating an IIS Hosted Handler with C# for Special Requests

After compilation, a DLL will be generated in the following directory within the project folder

Creating an IIS Hosted Handler with C# for Special Requests2. Configuring the IIS Hosted Handler

1. Copy the generated DLL file to the bin folder of the website directory

Creating an IIS Hosted Handler with C# for Special Requests

2. Open the IIS Manager and find “Handler Mappings” in the following directory

It is important to note that you must select the corresponding website program directory hereCreating an IIS Hosted Handler with C# for Special Requests

3. Add the hosted handler (there will be an error warning on the right)

Creating an IIS Hosted Handler with C# for Special Requests

4. Set the application pool to “Integrated Mode”

In the previous step, an error warning appeared.

This website is running in an application pool that operates in Classic Mode, so you can manage ISAPI extensions and native modules mapped to the path. You must manage the hosted handlers directly in the configuration file (system.web/httpHandlers). At this point, you can either change the website’s application pool to Integrated Mode or directly configure the mapping in the web.config file. The configuration format is as follows; we choose the former for configuration.

<handlers>  <add name="webRTC" path="webRTC" verb="*" type="HttpHandlerTest.okme" resourceType="Either" preCondition="integratedMode" /></handlers>

Creating an IIS Hosted Handler with C# for Special Requests

5. Add the program you wrote as a hosted handler

In the image below, the type HttpHandlerTest corresponds to the assembly name, and okme corresponds to the class name. If the request path is configured as webRTC, then any request path containing webRTC will be processed by this program.

Creating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special Requests

Configure request restrictions

It is particularly important to note that if this includes a folder, then create an empty folder in the website root directory named webRTC. Even if there are no actual files, accessing the path of this folder will allow entry into the program for processing, but this folder must be created, even if it contains no files.

Creating an IIS Hosted Handler with C# for Special RequestsCreating an IIS Hosted Handler with C# for Special Requests

Creating an IIS Hosted Handler with C# for Special RequestsAfter configuration, a new record will appear as shown in the image below:

Creating an IIS Hosted Handler with C# for Special Requests

Finally, access the program output in the browser. This is directly accessing the folder, but there are no files in the folder, yet this folder must exist.

Creating an IIS Hosted Handler with C# for Special Requests

Leave a Comment