The role of the. ashx file [original 〕

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.