Right-click the project in Vs and add a new project. The. ashx file is called "general processing" in the new project template.Program", What is the general processing program used?
We can simply understand it like this. Well, it doesn't need to be too complicated. the aspx file is used to process HTTP requests passed into the server, but it does not. the aspx file returns processing results and a large number of HTML files. It can return simple strings, images, and so on.
As you know, httphandle needs to be configured in the web. config file. What if it is not configured? Can I use it directly? Yes. Come and have fun.
1. Add a. ashx file to the project and rewriteCode, As follows:
Using system; using system. Web; public class imagehandle: ihttphandler {public void processrequest (httpcontext context) {context. response. contenttype = "image/JPEG"; string imgpath = @"~ /Images/10902.16.jpg "; // the corresponding image switch (context. request. querystring ["ID"]) {Case "1": context. response. writefile (context. server. mappath (string. format (imgpath, "1"); break; Case "2": context. response. writefile (context. server. mappath (string. format (imgpath, "2"); break; Case "3": context. response. writefile (context. server. mappath (string. format (imgpath, "3"); break; default: // the context of the first image is displayed by default. response. writefile (context. server. mappath (string. format (imgpath, "1"); break ;}} public bool isreusable {get {return true ;}}}
To put it simply, this class returns the corresponding image based on the request query parameters. Therefore, you can prepare three images for testing.
2. Complete the. ASPX page.
<% @ page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. CS "inherits =" _ default "%>
1
2
3
In this example, there are three links: 1, 2, and 3. The corresponding image is displayed after you click them.
.
We applied httphandle without configuring web. config. Below I will configure the Web. config file.
Locate the httphandlers section and add a node.
<Add verb = "*" Path = "loadimg" type = "imagehandle" Validate = "false"/>
Then, change the. ASPX page.
<SCRIPT type = "text/JavaScript"> function getimg (INDEX) {var myimg = Document. getelementbyid ('img '); myimg. src ='./loadimg? Id = '+ index ;}</SCRIPT>
At this time, the magic found invalid. What's going on? Is it because the file is not put into app_code?
Threw the. ashx file to the app_code directory. The result showed that the file still does not work.
As a result, I directly created a. CS file without using the. ashx file, copied the C # code in. ashx, changed the class name, and then ran it.
If this succeeds, does it mean that the httphandler configured in Web. config is invalid for the class defined in. ashx?