zoukankan      html  css  js  c++  java
  • Ashx文件的使用方法

    一提到Ashx文件,我们就会想到http handler以及图片加载(在之前我们一般使用ASPX或者Webservice去做),一般做法如下:

    Handler.ashx:
    <%@ 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:
    <img src="myHttpHander.ashx?id=123" width="20" height="20" />

    我们变通以下,发现其实除了可以输出图片以外,还可以输出文字:
    Handler.ashx:
    <%@ 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
    <script src="Handler.ashx"></script>
    也可以把.ashx当成css文件
    <link href="css/Handler.ashx" rel="stylesheet" type="text/css">
    xml文件
    orderDoc.load("Handler.ashx");

    还可以嵌入文字:
    Handler.ashx:
    <%@ 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" />

    当你希望从ashx或HttpHandler里访问你的Session时,你必须实现IReadOnlySessionState接口.

    代码:

    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"]);
       }
    }

  • 相关阅读:
    APP与智能手表是如何通信的【本文摘抄自深圳尚锐科技】
    flash添加超链接的办法
    NPOI使用手册
    如何在 SQL Server 实例之间传输登录和密码
    CentOS 7 上部署Mono 4 和Jexus 5.6
    django models 中choices之用法举例
    django中的objects.get和objects.filter方法的区别
    Django中的Model.objects.create() 和 Model() 的区别?
    python爬虫练手项目快递单号查询
    根据现有的数据库自动生成Django model
  • 原文地址:https://www.cnblogs.com/hxwzwiy/p/2412264.html
Copyright © 2011-2022 走看看