What is ashx and how to create it?

Source: Internet
Author: User
. AshxFile is used to write web handler. It is actually a hybrid file with HTML and C. Of course, you can use the. aspx file suffix. Use . AshxThis allows you to focus on programming without having to worry about related WEB technologies. . AshxIsReusable must be included, as shown in the following example.

<% @ Webhandler Language = "C #" class = "averagehandler" %>

Using system;
Using system. Web;

Public class averagehandler: ihttphandler
{
Public bool isreusable
{Get {return true ;}}
Public void processrequest (httpcontext CTX)
{
CTX. response. Write ("hello ");
}
}
The advantage of. ashx over. aspx lies in the lack of more HTML

Pay attention to the generic handler item in the Web application project template in vs2005, and find that it is A. ashx file, which is actually an httphandler. Later, I checked the. NET SDK document and found that ASP. net1.1 also supports. ashx, but no details are provided.

As we all know, httphandler is a method for completely customizing HTTP requests through the Web. config to define ASP.. Net runtime to filter out HTTP requests to be customized and send them to the Web. config.

Exploitation. the ashx file is a better method. This file is similar. aspx file, which can be used to call the httphandler class, thus eliminating the need for common. the control parsing and page processing process of the aspx page. This file is especially suitable for generating dynamic images, generating dynamic text, and other content.

The method is as follows:
First open a web project, and then use "add" --> "Add new project" of vs2003 solution Resource Manager in any directory. In the dialog box, select "Text File ", enter "textbuilder. ashx ".

Then, in the same directory, use solution Resource Manager, use "add" --> "add class", and enter "TextBuilder. ashx. cs" in the class file name ". It can be seen that its file naming rules are the same as that of. aspx files.

Enter the following code (The namespace is omitted) in the. cs file: using System. Web
Public sealed class TextBuilder: IHttpHandler
{
Public void ProcessRequest (HttpContext context)
{
Context. Response. ClearContent ();
Context. Response. ContentType = "text/plain ";
Context. Response. Write ("Hello World ");
Context. Response. End ();
}

Public bool IsReusable
{
Get {return true ;}
}
}

Then in "TextBuilder. enter the above Class call code in the first line of the ashx file: <% @ WebHandler language = "C #" Class = "MyNamespace. textBuilder "codebehind =" TextBuilder. ashx. cs "%>

Note that the complete name of the Class, including the namespace and Class name, must be entered in the Class item.

Finally, save and compile the project.

Run the IE test and enter the. ashx address.

You can see that the Response class has an OutputStream method that can output binary data streams to the client. Therefore, in my project, use this method in one. in ashx, The DundasChart control can be used to generate a very good statistical chart. It can be used to send binary data, which is convenient and does not need to be stored on the web. enter any configuration code in config.

. 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 the "General handler ". 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 write how to handle the request details in the ProcessRequest method, and IsReusable indicates whether other requests can use an instance of this class. We can temporarily ignore the IsReusable attribute. Focus on the 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

<% @ WebHandler Language = "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 (HttpContext Context)
{
// Obtain the physical path of the virtual directory.
String Path = Context. server. mappath ("");
// Obtain the binary data of the 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: <asp: Image id = "image1" runat = "server"Imageurl = "~ In/imagehandler. ashx "/> </div>, the imageurl points to the imagehandler. ashx file.

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.