Ashx File Usage Summary

Source: Internet
Author: User

When we mention the Ashx file, we will think of http handler and image loading (we used ASPX or Webservice before). The general practice is as follows:

Handler. ashx:
Copy codeThe Code is as follows:
<% @ WebHandler Language = "C #" Class = "Handler" %>
Using System;
Using System. IO;
Using System. Web;
Public class Handler: IHttpHandler {

Public bool IsReusable {
Get {
Return true;
}
}
Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "image/jpeg ";
Context. Response. Cache. SetCacheability (HttpCacheability. Public );
Context. Response. BufferOutput = false;
PhotoSize size;
Switch (context. Request. QueryString ["Size"]) {
Case "S ":
Size = PhotoSize. Small;
Break;
Case "M ":
Size = PhotoSize. Medium;
Break;
Case "L ":
Size = PhotoSize. Large;
Break;
Default:
Size = PhotoSize. Original;
Break;
}
Int32 id =-1;
Stream stream = null;
If (context. Request. QueryString ["PhotoID"]! = Null & context. Request. QueryString ["PhotoID"]! = ""){
Id = Convert. ToInt32 (context. Request. QueryString ["PhotoID"]);
Stream = PhotoManager. GetPhoto (id, size );
} Else {
Id = Convert. ToInt32 (context. Request. QueryString ["AlbumID"]);
Stream = PhotoManager. GetFirstPhoto (id, size );
}
If (stream = null) stream = PhotoManager. GetPhoto (size );
Const int buffersize = 1024*16;
Byte [] buffer = new byte [buffersize];
Int count = stream. Read (buffer, 0, buffersize );
While (count> 0 ){
Context. Response. OutputStream. Write (buffer, 0, count );
Count = stream. Read (buffer, 0, buffersize );
}
}
}

*. Aspx:

We can work with the following changes and find that in addition to outputting images, we can also output text:
Handler. ashx:
Copy codeThe Code is as follows:
<% @ WebHandler Language = "C #" Class = "Handler" %>
Using System;
Using System. Web;
Public class Handler: IHttpHandler {

Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/plain ";
Context. Response. Write ("alert ('Hi ')");
}

Public bool IsReusable {
Get {
Return false;
}
}
}

*. Aspx:
Alert pop-up
<Script src = "Handler. ashx"> </script>
You can also use. ashx as a css file.
<Link href = "css/Handler. ashx" rel = "stylesheet" type = "text/css">

Xml file
OrderDoc. load ("Handler. ashx ");

You can also embed text:
Handler. ashx:
Copy codeThe Code is as follows:
<% @ WebHandler Language = "C #" Class = "TestHandler" %>
Using System;
Using System. Web;
Public class TestHandler: IHttpHandler {

Public void ProcessRequest (HttpContext context ){
Context. Response. ContentType = "text/plain ";
Context. Response. Write ("document. write (\" Hello World \");");
}

 

Public bool IsReusable {
Get {
Return false;
}
}
}

*. Aspx:
<Script type = "text/javascript" src = "TestHandler. ashx"/>

When you want to access your Session from ashx or HttpHandler, you must implement the IReadOnlySessionState interface.

Code:
Copy codeThe Code is as follows:
Using System;
Using System. Web;
Using System. Web. SessionState;

Public class DownloadHandler: IHttpHandler, IReadOnlySessionState
{
Public bool IsReusable {get {return true ;}}

Public void ProcessRequest (HttpContext ctx)
{
Ctx. Response. Write (ctx. Session ["fred"]);
}
}

In fact, the learning idea should not be like this. In addition to the pictures above, we are all biased. Why is it so biased? Because the software is simple and practical, we simply regard the above as a technology without putting it into the software position :)
For specific purposes, you can refer to Rewirte. dll (This dll can enable the server to support pseudo-static)

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.