I am new in asp.net. i want to know about the generic handlers in asp.net and how and where it use?
Could you help me?
I am new in asp.net. i want to know about the generic handlers in asp.net and how and where it use?
Could you help me?
Generic handlers are the .NET components that implement the System.Web.IHttpHandler interface. Any class that implements the IHttpHandler interface can act as a target for the incoming HTTP requests. Page is also generic handler. In general generic handlers have an extension of ASHX.
You can find example here
Handlers are used when you want to avoid the overhead of a regular asp.net page. Practical examples include image processing or handling AJAX requests.
See Using HTTP Modules and Handlers to Create Pluggable ASP.NET Components.
Some ASP.NET files are dynamically generated. They are generated with C# code or disk resources. These files do not require web forms. Instead, an ASHX generic handler is ideal. It can dynamically return an image from a query string, write XML, or any other data.
Ashx File is nothing but just like an aspx page.They're equivalent to custom handlers written in C Sharp or Visual Basic.NET in that they contain classes that fully implement IHttpHandler. They're convenient in the same way ASPX files are convenient. You simply surf to them and they're compiled automatically.
When WebForms(aspx)to be used
Simple Html Pages
Asp.net Custom Controls
Simple Dyanamic Pages
When Handlers(ashx) to be used
Binary files
Dynamic Image Views
Performance critical web pages
xml files
Minimal Web Pages
ASHX Generic Handler is a concept to return dynamic content. It is used to return ajax calls, image from a query string, write XML, or any other data. I have used it to return MP4 file from query string. Please find the following code.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace ESPB.CRM.Web.UI.VideoUploading
{
public class FileCS : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int id = int.Parse(context.Request.QueryString["id"]);
byte[] bytes;
string contentType;
string strConnString = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
string name;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Name, Data, ContentType from VideoUpload where Id=@Id";
cmd.Parameters.AddWithValue("@Id", id);
cmd.Connection = con;
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
bytes = (byte[])sdr["Data"];
contentType = sdr["ContentType"].ToString();
name = sdr["Name"].ToString();
con.Close();
}
}
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
context.Response.ContentType = contentType;
context.Response.BinaryWrite(bytes);
context.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
Here I have created FileCS.ashx file. Where I inherit IHttpHandler interface. and wrote ProcessRequest(HttpContext context) function, which will run default while call the file. And context.Request.QueryString[] will get the parameter. Here I am passing id as parameter. IsReusable() function can be use for good performance.