Learn ASP.net HttpHandler in 2 steps

Hi this is Satalaj here In this article you will create HttpHandler using 2 steps. This article is for those who never tried or used HttpHandler in their web application.

Step 1
 
Create Web Application in ASP.net 2005 add new class name it as  SatHandler.cs
  it will get stored in to your app_code
  Inherit the properties of IhttpHandler in it below is code snippet  

public

class SatHandler:IHttpHandler

{

public SatHandler()

{

//

// TODO: Add constructor logic here

//

}

#region

IHttpHandler Members

public bool IsReusable

{

get { return true; }

}

public void ProcessRequest(HttpContext context)

{

context.Response.Write(context.Request.QueryString[

"user"]);

}

#endregion

}



Step 2

Open web.config file under <system.web> section

<

httpHandlers>

<

add verb="*" path="sat.sat" type="SatHandler"></add>

</

httpHandlers>

your web.config will look like this

<?

xml version="1.0"?>

<

configuration>

<

appSettings/>

<

connectionStrings/>

<

system.web>

<

compilation debug="true"/>

<

authentication mode="Windows"/>

<

httpHandlers>

<

add verb="*" path="sat.sat" type="SatHandler"></add>

</

httpHandlers>

</

system.web>

</

configuration>



Now run your web project and request sat.sat you will get whatever you passed in querystring

http://localhost:1279/WebSite3/sat.sat?user=Egg+Head

response will be Egg Head :)


This way write your API instead of calling Entire page life cycle and putting your api code in Page use HttpHandler in very simple way

-Satalaj

By Sat Sat   Popularity  (1897 Views)