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.
- <% @WebHandler Language = "C#"
- Class = "Handler" %>
-
- using System;
- using System.Web;
-
- public class Handler: IHttpHandler {
-
- public void ProcessRequest(HttpContext context) {
- context.Response.ContentType = "text/plain";
- context.Response.Write("Hello World");
- }
-
- public bool IsReusable {
- get {
- return false;
- }
- }
-
- }
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.
- function CallHandler() {
- debugger;
- var obj = {
- 'Email': $('#ContentPlaceHolder1_txtEmail').val(),
- 'pass': $('#ContentPlaceHolder1_txtPass').val(),
- 'User': $('#ContentPlaceHolder1_txtusername').val(),
- 'txtPhone': $('#ContentPlaceHolder1_txtPhone').val(),
- 'txtState': $('#ContentPlaceHolder1_txtState').val(),
- 'txtCity': $('#ContentPlaceHolder1_txtCity').val()
- }
- $.ajax({
- url: "Registration.ashx",
- contentType: "application/json; charset=utf-8",
- dataType: "json",
- data: obj,
- responseType: "json",
- success: OnComplete,
- error: OnFail
- });
- return false;
- }
-
- function OnComplete(Registration) {
- debugger;
-
- }
-
- function OnFail(result) {
- alert('Request Failed');
- }
Now we need to call the jQuery method on button click:
- <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:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using McnTracker.Models;
- using System.Data;
- using System.Data.SqlClient;
- using System.Web.Script.Serialization;
- using System.IO;
- using System.Web;
-
- namespace McnTrackerWeb {
-
-
-
- public class Registration1: IHttpHandler {
-
- public void ProcessRequest(HttpContext context) {
- McnTracker.Models.Registration rs = new McnTracker.Models.Registration();
- rs.UserName = context.Request.QueryString["User"].ToString();
- rs.Email = context.Request.QueryString["Email"].ToString();
- rs.pass = context.Request.QueryString["pass"].ToString();
- rs.Phone = Convert.ToInt64(context.Request.QueryString["Phone"].ToString());
- rs.States = context.Request.QueryString["States"].ToString();
- context.Response.ContentType = "text/plain";
- }
-
- public bool IsReusable {
- get {
- return false;
- }
- }
-
- }
- }
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.