Creating HttpHandler to Handle Custom extension Request in ASP.NET

No.of Views2437
Bookmarked0 times
Downloads 
Votes0
By  jalpesh   On  02 Jan 2011 21:01:43
Tag : ASP.NET , HttpHandlers
In this article I will explain how to create an HttpHandler to handle custom Request in ASP.NET. We all are knows existing extension in asp.net request such as .aspx, .html and more.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article I will explain how to create an HttpHandler to handle custom Request in ASP.NET. We all are knows existing extension in asp.net request such as .aspx, .html and more. If you are aware of httpHandler please read my previous article here. Now let’s create an http handler which will handle my custom extension. For that we need to create a http handlers class which will implement Ihttphandler. As we are implementing IHttpHandler we need to implement one method called process request and another one is isReusable property. The process request function will handle all the request of my custom extension.

so Here is the code for my http handler class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
 
namespace Experiement
{
   public class MyExtensionHandler:IHttpHandler
   {
       public MyExtensionHandler()
       {
           //Implement intialization here
       }
       
       bool IHttpHandler.IsReusable
       {
           get { return true; }
       }
 
       void IHttpHandler.ProcessRequest(HttpContext context)
       {
          string excuttablepath = context.Request.AppRelativeCurrentExecutionFilePath;
 
          if (excuttablepath.Contains("HelloWorld.dotnetjalps"))
          {
              Page page = new HelloWorld();
              page.AppRelativeVirtualPath = context.Request.AppRelativeCurrentExecutionFilePath;
              page.ProcessRequest(context);
 
          }
       }
   }
}

Here in above code you can see that in process request function I am getting current executable path and then I am processing that page.

Now Lets create a page with extension .dotnetjalps and then we will process this page with above created http handler. so let’s create it.

Image Loading

It will create a page like following.

Image Loading

Now let’s write some thing in page load Event like following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace Experiement
{
    public partial class HelloWorld : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("Hello World");
        }
    }
}

Now we have to tell our web server that we want to process request from this .dotnetjalps extension through our custom http handler for that we need to add a tag in httphandler sections of web.config like following.

<configuration>
   <system.web>
       <compilation debug="true" targetFramework="4.0" />
     <httpHandlers>
       <add verb="*" path="*.dotnetjalps" type="Experiement.MyExtensionHandler,Experiement"/>
     </httpHandlers>
   </system.web>
</configuration>

That’s it now run that page into browser and it will execute like following in browser

Image Loading

That's all. now see its running with custom extension.Happy programming.

 
Sign Up to vote for this article
 
About Author
 
jalpesh
Occupation-Software Engineer
Company-DotNetJaps
Member Type-Gold
Location-India
Joined date-08 May 2010
Home Page-http://www.dotnetjalps.com
Blog Page-http://www.dotnetjalps.com
I am jalpesh vadgamaa an Microsoft MVP for Visual C# and BrainBench Certified ASP.NET Developer having experience of five year in Microsoft .NET Technology.I am working as Project Leader in Mid Size company.My work area comprises of Enterprise Level projects using ASP.NET and other Microsoft .NET Technologies.Please feel free to contact me for any queries via posting comments on my blog I will try to reply as early as possible.
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top