* Example-use the. ashx file to process IHttpHandler to send text and binary data.

Source: Internet
Author: User
Recently, we are working on an internal system of our Organization, using Theme (Theme) features similar to Asp. Net 2.0. The core of this function is a dynamic CSS definition. Different CSS style texts are sent based on different theme content selected by the user.
This problem is well solved. In the But I didn't use this solution.

I noticed the Generic Handler item in the Web application project template in VS2005 and found 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.

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.