http handler example using asp.net
how to generate
random password in c# asp.net
All the request of asp.net is managed by a specialized component known
as an HTTP handler. Now as a web developer ,we might want to have some of our
own feature plugged in. we want to handle some knew kind of requests or perhaps
we want to handle an existing request ourselves to have more control on the
generated response.
Example : we may want to decide how to request for .jpg or .gif files
will be handles. we need HTTPHandler to have our feature in place..
Tags | asp handler,asp net web forms examples,asp net mvc project examples,asp c# examples,asp website examples,html in aspclassic asp example
For more details about handler you can take reference of MSDN.
References | https://msdn.microsoft.com/en-us/library/ms228090.aspx
Go for Client side Multiple parameter post :
Purpose
A handler is responsible for fulfilling requests from a browser.
Requests that a browser manages are either handled by file extension or by
calling the handler directly.
To create your first table below, complete the following steps:
Step 1 .
create database Test
create table employee
(
Id int identity,
name nvarchar(100)
)
insert into Employee
values ('vikas')
insert into Employee
values ('mohan')
insert into Employee
values ('ramesh')
insert into Employee
values ('rahul')
insert into Employee
values ('rakesh')
Step 2 :
To create your own HTTP handler, complete the following steps
randomqueryfromsql.ashx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Serialization;
namespace HttpHandler
{
///
/// Summary description for randomqueryfromsql
///
public class randomqueryfromsql : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int employeeId = 0;
int.TryParse(context.Request.QueryString["employeeId"], out employeeId);
string json = this.GetEmployeeJSON(employeeId);
context.Response.ContentType = "text/json";
context.Response.Write(json);
}
private string GetEmployeeJSON(int EmployeeId)
{
List
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT * FROM employee WHERE Id = @Id OR @Id= 0";
cmd.Parameters.AddWithValue("@Id", EmployeeId);
cmd.Connection = conn;
conn.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
employee.Add(new
{
CustomerId = sdr["id"],
Name = sdr["Name"]
});
}
}
conn.Close();
}
return (new JavaScriptSerializer().Serialize(employee));
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Step 3:
add below code in web.config for connection.
Step 4 : finally out put will display in below window.
Conclusion : we can use http handler for lots of work,where we want to eliminate the Page life cycle.
if we not use page lifecycle, then using http handler we can provide more performance of application.
http handler example using asp.net
Reviewed by Vikas Kumar Singh
on
December 12, 2017
Rating:
Nice blog. thank you for sharing this blog
ReplyDeletetableau course in Marathahalli
best tableau training in Marathahalli
tableau training in Marathahalli
tableau training in Marathahalli
tableau certification in Marathahalli
tableau training institutes in Marathahalli
Nice Post
ReplyDeleteDOT NET training in bangalore
dot net training institute in bangalore
dot net course in bangalroe
best dot net training institute in bangalore