HTTP Handlers
They are used to process individual endpoints.
Only one handler per request is used
Can be both asynchronous or synchronous
Earliest possible point where you have access to requests
HTTPHandlers are extension based. This means that for every extension .aspx .html etc there is a handler (some share handler).
The example used here is very simple and does nothing special except for displaying some static text. The reason is that ideally before attempting to do something elaborate one should check if it works by doing something simple especially while learning.
The examples here were created in ASP.MVC5 applications. The extension that is used is .kk
Synchronous HTTP Handler
The Solution name is HTTP_Handler
Step 1:
Ignore routing for .kk extension in App_Start/RouteConfig.cs (The reason is that we do not want MVC to try to interpret this file)
routes.IgnoreRoute("{file}.kk");
Step 2:
We create the class that will be our handler at the top level since this is an example and name it CustomHandler.cs
This can be achieved by inheriting IHttpHandler or by using IHttpHandlerFactory (The example uses IHttpHandler)
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace HTTP_Handler { public class CustomHandler : IHttpHandler { public void ProcessRequest(HttpContext context) { HttpRequest Request = context.Request; HttpResponse Response = context.Response; Response.Write("test"); } public bool IsReusable { get { return true; } } } }
Step 3:
We configure the application to use the handler (Integrated mode).
In web.config
<system.webServer> <handlers> <add verb="*" path="*.kk" name="CustomHandler" type="HTTP_Handler.CustomHandler"/> </handlers>
Step 4:
Build and Run
add hello.kk to the link and load it should print test on screen.
Asynchronous HTTP Handler
The Solution name is HTTP_Async_Handler
Step 1:
Ignore routing for .kk extension in App_Start/RouteConfig.cs (The reason is that we do not want MVC to try to interpret this file)
routes.IgnoreRoute("{file}.kk");
Step 2:
We create the class that will be our handler at the top level since this is an example and name it CustomHandler.cs
This can be achieved by inheriting
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace HTTP_Async_Handler { public class CustomHandler:IHttpAsyncHandler { // Long Process private static void generateNumbers(HttpContext context) { string s = ""; for (int i = 0; i < 1000; i++) { s = s + ' ' + i.ToString(); } context.Response.Write(s); } public static Action<HttpContext> _Handle = generateNumbers; public IAsyncResult BeginProcessRequest( HttpContext context, AsyncCallback cb, object extraData) { context.Response.Write(" BeginProcessRequest Executed"); return _Handle.BeginInvoke(context, cb,extraData); } public void EndProcessRequest(IAsyncResult result) { // Clean up code here } public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Write(" ProcessRequest Executed Will not execute"); } } }
Step 3:
We configure the application to use the handler (Integrated mode).
In web.config
<system.webServer> <handlers> <add verb="*" path="*.kk" name="CustomHandler" type="HTTP_Handler.CustomHandler"/> </handlers> </system.webServer>
Step 4:
Build and Run
add hello.kk to the link and load it should print test on screen.
Want to know more?
Investigate IHttpHandlerFactory
Classic mode & Integrated mode (IIS)