Loading ...

Custom handler to handle custom extensions | CodeAsp.Net

Custom handler to handle custom extensions

 /5
0 (0votes)

In this blog I will show how to make custom handler to handle custom extensions. You may be familiar with .ASPX, .ASHX, .ASMX etc extensions, now how to handle the situations where we have the custom extensions say ".MyOwnExtension" . Let's start making our own custom handler to handle the extensions ".raghav" . Create a ClassLibrary Project and then a custom class "RaghavHandler" inheriting from "IHttpHandler" Interface.




using System.Web;

namespace MyCustomHandlers
{
    public class RaghavHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpRequest httpRequest = context.Request;
            HttpResponse httpResponse = context.Response;
            // This handler executes whenever a file ending in .raghav is requested.
            httpResponse.Write("<html>");
            httpResponse.Write("<body>");
            httpResponse.Write("<h1> Hello this is my custom handler for handling files ending with .raghav . </h1>");
            httpResponse.Write("</body>");
            httpResponse.Write("</html>");
        }
        public bool IsReusable
        {
            // To enable pooling, return true here.
            // This keeps the handler in memory.
            get { return false; }
        }
    }

}

 

Compile the class library project and let's say the the compiled DLL is "MyCustomHandlers.dll". Now it's the time to use it. Create a web project or web application so as to use it and put the "MyCustomHandlers.dll" in bin folder of the WebProject. 





Now in the web.config of this project add the below line in httphandler section:

 

<add verb="*" path="*.raghav"
                 type="MyCustomHandlers.RaghavHandler,MyCustomHandlers, 
                 Version=1.0.0.0, Culture=neutral" />



Below is the screenshot to show you where to place that line:



That's it we are done. Let's test it and type the url in browser ending with ".raghav" extension.



As you saw we have started handling the custom extensions.

Do let me know your feedback, comments.

 

Comments (no comments yet)

Top Posts