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;
}
}
}