Call Generic Handler Using jQuery In ASP.NET

In this article I am going to explain how to call Generic Handler using jQuery and pass some data to the handler and recieve that data in Generic Handler.

Before starting working on the Generic handler first we need some information about what is Generic handler.

What is a Generic Handler?

Generic handler is a dynamic file and is generated with c# code and disk resource. Generic handler do not have web forms. Generic handler is also known as ASHX generic handler. It is useful when we want to return image from a query string,write XML and other Data.

We use the ASHX file in the URL and it dynamically returns data. Also, we use the query string.

Step 1

Start Visual Studio.


Step 2

Now for creating a website, click on File, go to New and click on the Website.


Step 3

Now add Web Form by right clicking on the website and then provide the name of the Web Form just added.


Step 4

After adding web form we will add the generic handler in the website, by right clicking on the website and add new item.



Step 5

The default handler contains the following code and the file extension of generic handler is .ashx.

  1. <% @WebHandler Language = "C#"  
  2. Class = "Handler" %>  
  3.   
  4. using System;  
  5. using System.Web;  
  6.   
  7. public class Handler: IHttpHandler {  
  8.   
  9.     public void ProcessRequest(HttpContext context) {  
  10.         context.Response.ContentType = "text/plain";  
  11.         context.Response.Write("Hello World");  
  12.     }  
  13.   
  14.     public bool IsReusable {  
  15.         get {  
  16.             return false;  
  17.         }  
  18.     }  
  19.   
  20. }  

Step 6

After adding the handler in the website, we call the generic handler by using jQuery. For calling the generic handler we need to create a function for calling the generic handler.

  1. function CallHandler() {  
  2.     debugger;  
  3.     var obj = {  
  4.         'Email': $('#ContentPlaceHolder1_txtEmail').val(),  
  5.         'pass': $('#ContentPlaceHolder1_txtPass').val(),  
  6.         'User': $('#ContentPlaceHolder1_txtusername').val(),  
  7.         'txtPhone': $('#ContentPlaceHolder1_txtPhone').val(),  
  8.         'txtState': $('#ContentPlaceHolder1_txtState').val(),  
  9.         'txtCity': $('#ContentPlaceHolder1_txtCity').val()  
  10.     }  
  11.     $.ajax({  
  12.         url: "Registration.ashx",  
  13.         contentType: "application/json; charset=utf-8",  
  14.         dataType: "json",  
  15.         data: obj,  
  16.         responseType: "json",  
  17.         success: OnComplete,  
  18.         error: OnFail  
  19.     });  
  20.     return false;  
  21. }  
  22.   
  23. function OnComplete(Registration) {  
  24.     debugger;  
  25.   
  26. }  
  27.   
  28. function OnFail(result) {  
  29.     alert('Request Failed');  
  30. }  

Now we need to call the jQuery method on button click:

  1. <asp:Button ID="AS" runat="server" Text="Submit" CssClass="mainBtn" OnClientClick="CallHandler();" />  

Step 7

Now we write the code for getting the value of Web Form in generic handler:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using McnTracker.Models;  
  6. using System.Data;  
  7. using System.Data.SqlClient;  
  8. using System.Web.Script.Serialization;  
  9. using System.IO;  
  10. using System.Web;  
  11.   
  12. namespace McnTrackerWeb {  
  13.     /// <summary>  
  14.     /// Summary description for Registration1  
  15.     /// </summary>  
  16.     public class Registration1: IHttpHandler {  
  17.   
  18.         public void ProcessRequest(HttpContext context) {  
  19.             McnTracker.Models.Registration rs = new McnTracker.Models.Registration();  
  20.             rs.UserName = context.Request.QueryString["User"].ToString();  
  21.             rs.Email = context.Request.QueryString["Email"].ToString();  
  22.             rs.pass = context.Request.QueryString["pass"].ToString();  
  23.             rs.Phone = Convert.ToInt64(context.Request.QueryString["Phone"].ToString());  
  24.             rs.States = context.Request.QueryString["States"].ToString();  
  25.             context.Response.ContentType = "text/plain";  
  26.         }  
  27.   
  28.         public bool IsReusable {  
  29.             get {  
  30.                 return false;  
  31.             }  
  32.         }  
  33.   
  34.     }  
  35. }  

Step 8

After writing the code in generic handler we run the website and check data posted to the handler or not.


Write the detail in webform and click on the submit button.

Now we use the break point to check the value or not in generic handler.


By clicking on the column, we see the value in the generic handler.

Summary

I hope this article is helpful for all those users who need to use the generic handler by using jQuery in ASP.NET.

Up Next
    Ebook Download
    View all
    Learn
    View all