asp.net *.ashx type of File Usage Description _ Practical Tips

Source: Internet
Author: User
Solution: Use the ashx file.
1. Use of ASHX handlers
First, we want to review the goal of using the Ashx file, what we do is to use the Ashx file in one address and dynamically return the content.
We will use the QueryString, the final address format (example):
Http://dotnetperls.com/?file=name
Start: With these steps you can add a new ashx file: Open your asp.net web site; Right-click the project selection
"Add New Item ..." will display a dialog box "Add New Item" and select "Generic Handler". At this point, you will
Will get a new ashx file.
2. Automatically generate code
What we need to be aware of is the code generated automatically in the Ashx file. It defines two parts of the IHttpHandler interface. Very important to a
The section is ProcessRequest (), which determines whether the ashx file is requested or displayed. You cannot modify this inherited interface or delete
In addition to its methods.
3. Mapping Handler
Mapping a older URL or a path to your new ashx file is usually desirable. For backwards compatibility and optimization of search engines, you get
Hopefully this handler can take over an old URL. How to achieve it? With urlmappings;
Copy Code code as follows:

<system.web>
<urlmappings enabled= "true" >
<add url= "~/default.aspx" mappedurl= "~/handler.ashx"/>
</urlMappings>

URL mappings: The web.config configuration above will automatically connect a URL to another URL. Now, when Default.aspx is requested
, your ashx file will take over. This means that you can map default.aspx to your handler.
4. Add a picture
Here, we talk about what you can do with ashx files. Find a picture that you like. Add it to your site project. For example
, I chose a picture "Flower1.png". Below, we will use this image in the ashx file.
5. Modify the Ashx file
There are two parts of your ashx file, where we have to modify the ProcessRequest () method. We can change this file.
ContentType and response content. Modify your ashx file in the following way.
~ ~ ~ ASHX code-behind file (C #) ~ ~ ~
Copy Code code as follows:

Using System;
Using System.Web;
public class Handler:ihttphandler {
public void ProcessRequest (HttpContext context) {
Comment out these lines:
Context. Response.ContentType = "Text/plain";
Context. Response.Write ("Hello World");
Context. Response.ContentType = "Image/png";
Context. Response.WriteFile ("~/flower1.png");
}
public bool IsReusable {
get {
return false;
}
}
}
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.