Summary of the use of ashx files _ practical skills

Source: Internet
Author: User
Tags httpcontext

When it comes to ashx files, we think of HTTP handler and picture loading (which we typically do with aspx or WebService), as follows:

HANDLER.ASHX:

Copy Code code 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 have adapted the following, found that in addition to the output of the picture can also be output text:
HANDLER.ASHX:

Copy Code code 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:
Pop-up alert
<script src= "Handler.ashx" ></script>
You can also think of. 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 Code code 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 Code code as follows:

Using System;
Using System.Web;
Using System.Web.SessionState;

public class Downloadhandler:ihttphandler, ireadonlysessionstate
{
public bool IsReusable {get {true;}}

public void ProcessRequest (HttpContext ctx)
{
CTx. Response.Write (CTX. Session["Fred"]);
}
}


In fact, the idea of learning should not be like this, in addition to the picture, we have to use the partial, why use the biased, because the software is simple, practical, we just think of the above as a technology and did not put it to the status of the software to consider:
Specific use, you can refer to Rewirte.dll (this DLL, you can make the server 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.