Ashx is used to write web handler. It is generally used to generate dynamic images, generate dynamic text, and other tasks that do not need to be processed back.
Example 1: for example, to create a new method of Ajax without refreshing, I use jquery for Ajax requests, as shown below:
-
JScript code
-
<script type="text/javascript">function onAjaxRequest(){ $.get("AjaxServer.ashx?a=1&b=2", function callBack(data) { alert(data); });}</script><input type="button" value="Click me" onclick="onAjaxRequest();"/>
-
C # code
-
<%@ WebHandler Language="C#" Class="AjaxServer" %>using System;using System.Web;public class AjaxServer: IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; int a = int.Prase(context.Reqeust.QueryString["a"]); int b = int.Prase(context.Reqeust.QueryString["b"]); context.Response.Write((a+b).ToString()); } public bool IsReusable { get { return false; } }}
In this way, the above is a simple use of ashx. In actual development, it is very useful. It directly implements the ihttphandler interface. In fact, aspx also implements this interface.
Example 2:
Code
<% @ Webhandler Language = "C #" class = "exit" %>
Using system;
Using system. Web;
Using system. Web. sessionstate;
Public class Exit: ihttphandler, irequiressessionstate
{
Public void processrequest (httpcontext context)
{
Context. response. Cookies ["mycookies"] ["myid"] = "";
Context. response. Cookies ["mycookies"] ["myusername"] = "";
Context. response. Cookies ["mycookies"]. expires = datetime. Now. adddays (-1 );
Context. response. Redirect ("default. aspx ");
}
Public bool isreusable {
Get {
Return false;
}
}
}
Complete example:
Code
<% @ Webhandler Language = "C #" class = "handler" %>
Using system;
Using system. Web;
Using system. Data;
Using system. Data. sqlclient;
Using system. text;
Public class handler: ihttphandler {
Public void processrequest (httpcontext context)
{
/* --------------------------- Custom code ----------------------------*/
String STR = @ "Server =.; uid = sa; Password = 123456; database = northwind ";
// Retrieve the table in the database
Sqlconnection con = new sqlconnection (STR );
Dataset DS = new dataset ();
Sqldataadapter SDA = new sqldataadapter ();
SDA. selectcommand = new sqlcommand ("select * from employees", con );
Con. open ();
SDA. Fill (DS );
Con. Close ();
// Fill the table in the datatable
Datatable dt = Ds. Tables [0];
// Construct the output stream format
Stringbuilder strpart = new stringbuilder ();
Strpart. append ("<Table> ");
For (INT I = 0; I <5; I ++)
{
Strpart. appendformat ("<tr> <TD> <a href = 'default. aspx? Id = {0} '> {1} </a> </TD> </tr> ", DT. rows [I] ["employeeid"], DT. rows [I] ["lastname"]);
}
Strpart. append ("</table> ");
String S = strpart. tostring ();
// Streams are returned to the page according to the HTTP local request
Context. response. Write (s );
}
Public bool isreusable
{
Get {
Return false;
}
}
}
Note: In the ashx file, context must be added to response, request, server. mappath, and so on.