Application of ashx files

Source: Internet
Author: User

In ASP. NET, there is a file ashx in this format. What is the purpose? If you want to create an ASP. NET file that is not An ASPX file, it can dynamically return an image, XML file, or other non-HTML files. Use the ashx file.
The following describes how to use it:
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. automatically generate 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;
<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 talk 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 this file's
Contenttype and response content. Modify Your ashx file as follows.
~~~ Ashx code-behind file (C #)~~~
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 ");
}
Public bool isreusable {
Get {
Return false;
}
}
}
--------------------------------------------
Handler. ashx file functions (go to) 2010-01-08 14: 43asp. net2.0 person Web site: read images from the database in binary format

In ASP. NET person Web site starter kits, images are stored in the database in binary format. Microsoft provides handler. ashx to read the images.

First, the reference to the image you see is similar to the following:

That is, every image you see is dynamically generated by handler. ashx. Therefore, when you save the "image" You see, its name is handler. ashx.

Use handler. ashx instead of handler. it is not certain to use aspx to reference images. In other words, you can change the file to handler. there is no essential difference between aspx, but according to Microsoft documents, use *. the document ratio of ashx as the extension *. the document Performance of aspx is high because it reduces the generation of the Control tree.

----------

Here is the handler. ashx code:

<% @ Webhandler Language = "C #" class = "myhandle" %>

Using system;

Using system. Web;

Using system. drawing;

Using system. Drawing. imaging;

Using system. IO;

Using system. Web. caching;

Using system. Data;

Public class myhandle: ihttphandler {

Public void processrequest (httpcontext context ){

// Retrieve from database

Memorystream MS = JSZ. sqlserverdal. testdal. getimage (1 );

Datatable dt = JSZ. sqlserverdal. testdal. gettable (1 );

Byte [] image = (byte []) dt. Rows [0] ["photo"];

Memorystream MS = new memorystream (image, 0, image. Length );

If (MS! = NULL)

{

// Get the memorystream size of the image

Int buffersize = (INT) ms. length;

// Create a buffer

Byte [] buffer = new byte [buffersize];

// Call memorystream. Read, read from memorystream to buffer, and return count

Int countsize = Ms. Read (buffer, 0, buffersize );

// Return the Image Buffer

Context. response. outputstream. Write (buffer, 0, countsize );

}

}

Public bool isreusable {

Get {

Return false;

}

}

}
 
------------------------

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.