. Ashx application:
Process tasks that do not need to be processed back, such as generating dynamic images and generating dynamic text
Handle Ajax requests
You can use the ashx file to create a web service. Similar to Web servers. For example, transmitting data in JSON format
This can be used for lightweight information interaction, and is not as complex as Aspx.
. The ashx file has a disadvantage. It is very troublesome to process the control's sending back event. For example, if it is used to generate the DataGrid list, it does not work, but it is required to process data sending back. the features of the ASPX page can only be manually processed. Therefore,. ashx is generally used to output projects that do not require sending back.
Use "general processing" Program ". If you are using vs2005, you can see the "General handler" in adding a new project. Its suffix is. ashx. What is it? In fact, it is similar to. aspx. First, ask. How does aspx work? As you may know,. aspx can process external incoming requests, and then it can process this request and generate an HTML file to return the result. This is a typical way to process external requests .. Aspx is specially designed to handle "typical" requests. So what should we do if we need to process an external request and a custom request? (That is, the "typical" method is not used for processing ).. Ashx can help you do this.
First, you will find <% @ webhandler Language = "C #" class = "imagehandler" %>. Think about whether an ASP. NET page has something similar. In fact, it indicates that the current file can process an external request. Of course, it won't work.
Next, the key thing is the class created below, which implements a key interface: ihttphandler. This interface indicates the method in which you will process external requests. There is a method and attribute to be implemented. You can Processrequest Method, and isreusable indicates whether other requests can use an instance of this class. We can temporarily ignore the isreusable attribute. Focus Processrequest Method. In Processrequest The context parameter is of the httpcontext type. The context object provides reference to internal server objects (such as request, response, session, and server) used to provide services for HTTP requests. That is, we can access several of our server objects.
Now let's look at a simple example.
Please put an image in your own web site folder. My idea is that I first read an image into a binary data and then convert the binary data into an image. You need to create two files. A. aspx file and the. ashx file that we want to use now.
File imagehandler. ashx
<% @WebhandlerLanguage = " C # " Class = " Imagehandler " %>
Using System;
Using System. Web;
/// <Summary>
/// This is a general processing program without any implementation.
/// </Summary>
Public Class Imagehandler:Ihttphandler {
Public Void Processrequest(HttpcontextContext)
{
// Obtain the physical path of the virtual directory.
String Path = Context. server. mappath ( "" );
// Obtains the binary data of an image file.
Byte []Datas = System. Io. file. readallbytes (Path + " \ U1513.jpg " );
// Write binary data to the output stream.
Context. response. outputstream. Write (datas, 0 ,Datas. Length );
}
Public Bool Isreusable {
Get {
Return False;
}
}
}
default. aspx file
note the above Code : imageurl = "~ In/imagehandler. ashx "/ , the imageurl points to the imagehandler. ashx file.