ASP.NET Advanced Generic Handler ASHX Introduction

Generic handlers are the .NET component, In general generic handlers have an extension of ASHX.
I see a lot of people using pages to process AJAX requests when we can use this much less expensive endpoint. This is an completely worked out Generic Handler that truly knows how to handle your http (AJAX) requests.
Using the code-
1-Create a new Generic Handler
2-Clear everything inside the handler class
3-Inherit from my Handler class
4-DONE! Now you only need to add your methods.

HTML

<!DOCTYPE html>
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<title></title>
<!– Include your Jquery file here –>
http://jquery-2.1.1.js

function SaveTextBoxData() {
var objData = new Object();
objData.userPass = $(“#txtname”).value;
$.ajax({
cache: false,
type: “POST”,
url: “../Handler1.ashx?ACTION=savenamedata”,
data: objData
}).done(function (data) {
alert(data);
});
}

</head>
<body>

Save

</body>
</html>

 

Handler1.ashx

<%@ WebHandler Language=”C#” Class=”Handler1″ %>

using System;
using System.Web;

public class Handler1 : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
string retvalue = “”;
string ACTION = context.Request.QueryString[“action”];
switch (ACTION)
{
case “savenamedata”: retvalue = SaveNameData(context); break;
}

context.Response.ContentType = “Application/Json”;
context.Response.Write(retvalue);
}
public bool IsReusable
{
get
{
return false;
}
}
public string SaveNameData(HttpContext context)
{
string rest=””;
var obj = context.Request.Form;
rest = obj[“userPass”].ToString();

return rest;
}
}

 

Leave a comment