ASP. NET gossip: what is the use of the ashx file?

Source: Internet
Author: User

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?

 

 

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.