Summary of the use of ashx files

Source: Internet
Author: User

  This article mainly introduces the use of ashx files. Need friends can come to the reference, I hope to help you.

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:   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;  }} PU Blic 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;   &N Bsp;case "M":     size = Photosize.medium;     break;    case "L":     size = Photosize.large;     break;    default:     size = photosize.original;     break;  }    Int32 id =-1; &nbsP 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: <img src= "myhttphander.ashx?id=123" width= "" height= "/>  " we modify the following to find that in fact except Can output a picture, but also output text: handler.ashx:     Code as follows: <%@ WebHandler language= "C #"lass= "Handler"%> using System; Using System.Web; public class Handler:ihttphandler {      public void ProcessRequest (HttpContext context) {  &NBSP ;     Context. Response.ContentType = "Text/plain";         context. Response.Write ("Alert (' Hi ')");    }       public bool IsReusable {        get {      &NBSP ;     return false;        }    } {    *.aspx: Pop-up alert <script src= "Handler.ashx" ></script > can also take. ashx as a CSS file <link href= "css/handler.ashx" rel= "stylesheet" type= "Text/css" >   XML File Orderdoc.load ("Handler.ashx");   can also embed text: handler.ashx:   Code as follows: <%@ WebHandler language= "C #" class= "Testhandler"%> using System; Using System.Web; public class Testhandler:ihttphandler {      public void ProcessRequest (HttpContext context) {  & nbsp     Context. RespOnse. ContentType = "Text/plain";         context. Response.Write ("document.write (" Hello World); ");    }           public bool IsReusable {        get {  &NBSP ;         return false;        }    } {    *.aspx: <script type= "Text/javascript src=" Testhandler.a Shx "/>   When you want to access your session from ASHX or HttpHandler, you must implement the Ireadonlysessionstate interface.   Code:   Code is as follows: using System; Using System.Web; Using System.Web.SessionState;   public class Downloadhandler:ihttphandler, ireadonlysessionstate {   public bool IsReusable {get {RE Turn true; }      public void ProcessRequest (HttpContext ctx)    {       CTX. Response.Write (CTX. Session["Fred"]);   &NBSP}}     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 without putting it to the status of the software to consider: For specific purposes, we can refer to Rewirte.dll(This DLL, which enables 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.