zoukankan      html  css  js  c++  java
  • .ashx文件

      ashx文件又叫一般处理应用程序,它和aspx文件表面上相差一个字母,前者是h(我想是handler),后者是p(我猜是pages).

      ashx文件处理传入服务器的http请求,可以返回xml,图片,文件,字符串等等。不像aspx文件返回整个页面,这样

    当你不需要返回一个页面时,使用ashx文件效率要高。

      下面通过一个例子来看一下:

    首先新建一个ashx文件:

    Handler1.ashx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace WebApplication2
    {
        /// <summary>
        /// Handler1 的摘要说明
        /// </summary>
        public class Handler1 : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                //根据不同响应内容设置context.Response.ContentType
                context.Response.ContentType = "image/jpeg";
                string id = context.Request.QueryString["id"];//获取请求参数
                string dir = "/images/{0}.jpg";
                switch (id)
                {
                    case "1":
                        context.Response.WriteFile(context.Server.MapPath(string.Format(dir, "1")));
                        break;
                    case "2":
                        context.Response.WriteFile(context.Server.MapPath(string.Format(dir, "2")));
                        break;
                    case "3":
                        context.Response.WriteFile(context.Server.MapPath(string.Format(dir, "3")));
                        break;
                }
              
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }

    然后在同目录images下添加3张图片,新建下面的aspx文件:

    WebForm1.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    </head>
    <body>
       
          <form id="form1" runat="server">  
        <div>  
            <div>  
                <img id="img" alt="图片替代文本" src="/images/1.jpg" height="300px" />  
            </div>  
            <a href="#" onclick="GetImg(1)">1</a>   
            <a href="#" onclick="GetImg(2)">2</a>   
            <a href="#" onclick="GetImg(3)">3</a>  
        </div>  
        </form> 
    
    
    
    
    </body>
    </html>
    
    <script type="text/javascript" language="javascript">
    
        function GetImg(index) { //js函数定义形式
            var img = document.getElementById("img");
            img.src = "/Handler1.ashx?id=" + index;
        };
    
    
    
    </script>

    然后单击2就好切换到第二张图片。

    Handler1.ashx文件中有一个content变量,它包含了Request,Response,Server等等对象。

    例子出处:http://blog.csdn.net/tcjiaan/article/details/7097180

  • 相关阅读:
    js入门简单介绍
    HTML中input参数,多行文本textarea说明,以及获取和设置的方法
    css属性相对定位,绝对定位,固定定位
    Django框架(二十七)—— ContentType组件
    Django框架(二十八)—— Django缓存机制
    Django框架(二十五)—— Django rest_framework-路由控制与响应器
    Django框架(二十六)—— Django rest_framework-分页器与版本控制
    Django框架(二十三)—— Django rest_framework-解析器
    Django框架(二十四)—— Django rest_framework-视图组件
    Django框架(二十二)—— Django rest_framework-频率组件
  • 原文地址:https://www.cnblogs.com/wang7/p/2735536.html
Copyright © 2011-2022 走看看