Asp.net *. ashx file usage instructions

Source: Internet
Author: User

Solution: Use the ashx file.
1. Use ashx handlers
First, we should review the goal of using the ashx file. What we need to do is to use the ashx file in an address and dynamically return the content.
Querystring is used. The final address format is (example ):
Http://dotnetperls.com /? File = Name
Start: you can add a new ashx file through these steps: Open your ASP. NET web site; Right-click the project and select
"Add new item..."; a "Add new item" dialog box is displayed, and "generic handler" is selected ". Now
A new ashx file is generated.
2. Automatic Generation Code
We need to note the code automatically generated in the ashx file. It defines two parts of the ihttphandler interface. Important 1
The part is processrequest (), which determines whether the ashx file is requested or displayed. You cannot modify this inherited interface or delete
Except its method.
3. Handing Handler
It is generally advisable to map an older URL or path to your new ashx file. To backward compatibility and Optimize search engines, you can obtain
I hope this handler can take over an old URL. How to implement it? Use urlmappings; Copy code The Code is 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 one URL to another. 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 an image
here, we are talking about what you can do with the ashx file. Find an image you like. Add it to your website project. For example,
I chose an image named "flower1.png ". Next, we will use this image in the ashx file.
5. Modify the ashx file
there are two parts in your ashx file. Here, we must modify the processrequest () method. We can change the
contenttype and response content of this file. Modify Your ashx file as follows.
~~~ Ashx code-behind file (C #)~~~ copy Code the code is as follows: using system;
using system. web;
public class handler: ihttphandler {
Public void processrequest (httpcontext context) {
// comment out these lines first:
// context. response. contenttype = "text/plain";
// context. response. write ("Hello World");
context. response. contenttype = "image/PNG ";
context. response. writefile ("~ /Flower1.png ");
}< br> Public bool isreusable {
get {
return false;
}< BR >}

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.