Home
 
  

Jquery ajax using Asp.net generic HTTP Handlers

April 4, 2014

Handlers makes life easier for using pure JavaScript or jQuery Ajax, I think its much cleaner way to use Handlers to call Ajax. jQuery is most popular JavaScript library in the world and its very easy to use.

We can create handlers in to ways one is Generic handler and second is custom handler. Generic handlers have pre mapping as *.ashx with custom handler we need to add handler mapping in web.config.

Handler are faster then ASP.NET WebPage, because of small life cycle (only one method for executing). Handlers never generate ViewState also.

Every handler must be implemented System.Web.IHttpHandler, and need to implement following method and property

public class SampleHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.Write(_jsserializer.Serialize(_productentity.GetProduct()));
}
public bool IsReusable
{
get
{
return false;
}
}
}

I already mentioned that we can use handlers two types. let understand it.

Generic handler (.ashx) :

It is pre mapped handler and .ashx is a mark up file like .aspx. for adding Generic handler follow these steps.

Right click on Project > Add > New Item > Web > Generic Handler (enter name of handler)

generic handler contains two file (one is mark and second is code behind), in markup file VS generate following directive

<%@ WebHandler Language="C#" CodeBehind="getproducts.ashx.cs" Class="IAnswerable.HttpHandlersTutorial.GenericHandler.getproducts" %>

we can remove code behind file and add code below directive (it is same as add runat server script on .aspx page)., but this will not be pre compiled, let understand with getproduct handler

<%@ WebHandler Language="C#" CodeBehind="getproducts.ashx.cs" Class="IAnswerable.HttpHandlersTutorial.GenericHandler.getproducts" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using IAnswerable.HttpHandlersTutorial.Entity;
using System.Web.Script.Serialization;
namespace IAnswerable.HttpHandlersTutorial.GenericHandler
{
public class getproducts : IHttpHandler
{
ProductEntity _productentity = new ProductEntity();
JavaScriptSerializer _jsserializer = new JavaScriptSerializer();
public void ProcessRequest(HttpContext context)
{
context.Response.Write(_jsserializer.Serialize(_productentity.GetProduct()));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}

We can check result of this direct from browser just type url of this (on my end it is [http://localhost:2312/GenericHandler/getproducts.ashx](http://localhost:2312/GenericHandler/getproducts.ashx/)), we will use this handler in jQuery after Custom handler.

Custom handler :

for adding this kind of handler follow following steps

Right click on Project > Add > New Item > Web > ASP.NET Handler

enter name according to your requirements, it will be looked like just simple class

My product handler is (I have coded same functionality as generic handler)

using System;
using System.Web;
using System.Web.Script.Serialization;
using IAnswerable.HttpHandlersTutorial.Entity;
namespace IAnswerable.HttpHandlersTutorial.CustomHandler
{
public class Products : IHttpHandler
{
#region IHttpHandler Members
ProductEntity _productentity = new ProductEntity();
JavaScriptSerializer _jsserializer = new JavaScriptSerializer();
public void ProcessRequest(HttpContext context)
{
context.Response.Write(_jsserializer.Serialize(_productentity.GetProduct()));
}
public bool IsReusable
{
get
{
return false;
}
}
#endregion
}
}

We cant access this handler without adding Handler mapping in web.config, open Web.config and add following mapping.

If you are using IIS6 then add following under system.web > httpHandlers

<add path="getproducts.pr" verb="*" type="IAnswerable.HttpHandlersTutorial.CustomHandler.Products"/>

and if using II7 or 7.5 then

<add name="getproducts" path="getproducts.pr" verb="*" type="IAnswerable.HttpHandlersTutorial.CustomHandler.Products"/>

in mapping

• path : url of handler (dont have added getproducts.pr)

• verb : accepted http methods (GET, POST, HEAD etc.) * means all

• type : Type of handler

• name (in IIS 7 or above) : just unique name.

We can access this handler from browser directly (my end url is [http://localhost:2312/getproducts.pr](http://localhost:2312/getproducts.pr/))

Handlers useful in REST api (not recommended) and in Ajax, Let we understand how to use Handlers through jQuery. jQuery is very easy to use and lightweight.

first add jQuery I recommend use google cdn for this

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

then use this jQuery ajax code, click event of a button

<script>
$(document).ready(function(){
$("#loadpage").click(function(){
$.getJSON("/getproducts.pr",function(products){
$.each(products, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
// created list and append to products div.
$('<ul/>', { 'class': 'my-new-list',html: items.join('')}).appendTo('#productsdiv');
});
});
});
</script>

You can use various ajax methods as your requirements.

Stackoverflow reference

Govind Malviya

Govind Malviya

Director of Product Development @LoginRadius

 

© 2020