How to create an HTTP Handler Application in asp.net

By
Htttp Handlers are used to handle the user request for web application resources.It is also known as request-response model of web application.For each request type,there is a specific event handler to handle the request and send back the corresponding response object.In below example i am going to implement the two HTTP Handler.Here i have used following elements in this application :
Namespace :-Student ,Employee  
 Class  :-   Handler 1 and Handler 2
 Path :- class 1.test and  class 2.test  respectively
I have explained each steps below ,which are given below:-
Step 1:-  First open your visual studio--> File --> New -->Website-->Select ASP.NET Empty Website -->OK--> Open Solution Explore -->Add a Web form(home.aspx) .
Step 2:- Now again open Solution Explorer -->Add two class(Handler 1,Handler 2)in APP_Code Folder-->write the following codes for  each class as given below:-

For Handler 1 class:-

using System;
using System;
using System.Web;
/// 
/// Summary description for Handler1
/// 
namespace student
{
    public class Handler1 : IHttpHandler
    {
       
        public void ProcessRequest(System.Web.HttpContext context)
        {
            HttpResponse Response = context.Response;
  Response.Write("I am HTTP Handler please visit http://www.msdotnet.co.in/ for more real Applications");
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}


For Handler 2 class:-

using System;
using System.Web;
/// 
/// Summary description for Handler2
/// 
namespace Employee
{
    public class Handler2 : IHttpHandler
    {
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
        public void ProcessRequest(System.Web.HttpContext context)
        {
            HttpResponse Response = context.Response;
   Response.Write("I am second HTTP Handler visit:http://www.msdotnet.co.in/ for more info.");
        }
    }
}


Step 3:-Now open web.config file-->write the following codes as given below:-
Syntax of HTTPHandlers code in web.config file:- 

<httpHandlers>>
<add verb="supported http verbs" path="Example1.test" type="namespace1.classname1" />
<add verb="supported http verbs" path="Example2.test" type="namespace2.classname2" />
<httpHandlers>
Original Application codes:-


<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
 <system.web>
  <httpHandlers>
  <add verb="*" path="class1.test" type="student.Handler1"/>
                <add verb="*" path="class2.test" type="Employee.Handler2"/>
  </httpHandlers>
  <compilation debug="true"/>
</system.web>
</configuration>


Step 4:- Now Run the Application(press F5)-->Put your path Name from web.config file after the URL as shown below:-
For Handler 1 class:-


class 1_output
For Handler 2 class:-


class 2_output


For More:-
  1. Multithreading in c#
  2. Validation control in c#
  3. Create captcha image
  4. Transaction in sql server
  5. Exception handling in c#
  6. How to host asp.net website on server free 
For whole Application:-
    Download

0 comments:

Post a Comment

Powered by Blogger.