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)