ASP. NET ashx general processing program tutorial

Source: Internet
Author: User

This document is translated into the official documents of http://www.dotnetperls.com/ashx. The objective is to deepen your understanding of the ashx file.

You do not want to create a common ASP. NET web form page. A query string is used to return a dynamic image, XML, or non-HTML webpage. This is a simple tutorial on using ashx (general processing program) written in C # programming language.

 

 

Introduction

First, let's review the purpose of using the ashx file. Maybe we want to use this ashx file in the URL to dynamically return data content. The URL query string we use is as follows:
Http://www.dotnetperls.com /? File = Name.

Entry: how to add an ashx file?

Open your asp.. Net website, click the "Add new project" menu on the website, open the "Add new project" window, select the General handler (generic handler ), then you will see a handler with some code. the ashx file.

Automatically generated code

We noticed that the automatically generated code in the ashx file defines two methods of the ihttphandler interface. The most important method is processrequest (). Whether it is request or output, this method will be called. You should not modify any default inherited interface members.

URL ing

Generally, it is feasible to replace the old URL or path with a new URL. For backward compatibility and search engine optimization, you may want to replace an old URL address with a new handler on your website. URL ing can solve this problem. Of course, you can use a more complex path to retry the method.

Some code in Web. config

<System. Web>

<Urlmappings
Enabled = "true">

<Add
Url = "~ /Default. aspx"
Mappedurl = "~ /Handler. ashx "/>

</Urlmappings>

........

</System. Web>

 

The above code automatically transfers a connection to another one. When the default. aspx page is requested, the handler. ashx file will be replaced. This means that the default page of your directory will automatically navigate to the general handler.

Add sample image

Here, you may use the ashx file to process image files. Find your favorite image on the desktop or on the network and add it to your project. For example, the image I selected is "flower1.png", which will be used in the ashx file below.

Modify the handler. ashx File

Your handler has two methods. We must modify the processrequest () method to modify the contenttype and output content of the file. You can use the following similar code to modify the file name and contenttype attributes of your image.

Public
Void processrequest (httpcontext context)

{

Context. response. contenttype =
"Image/PNG ";

Context. response. writefile ("~ /Images/flower1.png ");

}

 

Public
Bool isreusable

{

Get

{

Return
False;

}

}

Test the processing program

Test the ashx file locally. Click the green button of the website. You will see your image file in the browser. This is the image returned by the processing program.

 

Add feature

So far, the above example has no effect. It is mainly used to process an image file through ashx. You can add any logic code or logical reference to the ashx file. Developers usually need to use the query string set of requests. You can request a query string like the method used in the aspx file: request. querystring ["file"].

 

 

The above code receives a request based on the query string set and then returns different image files. The query string is used to return one of the two images.

For example:

Url = http://www.dotnetperls.com /? File = logo

File query string: logo

File written: logo1.png

Url = http://www.dotnetperls.com /? File = flower

File query string: Flower

File written: flower1.png

Test query string

So many preparations have been made. Start testing! Open your browser and add the preceding query string to the URL path. You will see ASP. NET from default. aspx page navigation to handler. ashx page. In addition, the system returns an appropriate file by obtaining the query variable.

Purpose

The code can be used as a counter of the number of visitors or a counter recommended by the log. Because of the difference between browsers and bots, this will provide a more accurate number of accesses than server logs.

Performance

You may want to know whether the use of the ashx file will bring some performance advantages or changes? The ashx file is not complex and does not involve calling more events. As you imagine, processing more than 10 events for a single request consumes more performance than processing one event. So, use the ashx file whenever possible, and it will bring some performance advantages.

Select a handler

When can I use a client to process programs and web form files? Generally, the processing program has more advantages in processing binary data, while the web form is better in rapid development.

 

Control tree

In the ASP. NET Framework, web forms use a concept called Control tree (Web pages are stored in an object model ). When you do not need the control tree of the client or the entire website architecture, using a general processing program will bring better performance advantages and simple code debugging.

 

Isreusable attributes

I do not know the role of the isreusable attribute in ASP. NET. By reading the table name, he can improve performance and reduce memory pressure without disrupting repeated program processing.

 

Summary

In this article, I will give you how to use the ashx custom processing program on your website. After modification, you can enter more important websites. Combined with custom query string URL ing, You can greatly simplify and optimize your website background code

Related Article

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.