---
title: "ASP.NET大闲话：ashx文件有啥用"
author: "mmdev"
date: "2011-12-23T09:06:00Z"
tags: []
slug: "asp-net-ashx"
draft: false
lastmod: "2012-10-05T09:01:29Z"
id: 1690016
url: "https://mmdev.iteye.com/blog/1690016"
---

在VS中右击项目，添加新项，我们找到.ashx文件在新建项模板中叫做“一般处理程序”，那么这个一般处理程序用来干吗的呢？

我们可以这样地简单理解，嗯，不需搞得太复杂，它就类似.aspx文件，用于处理传入到服务器的HTTP请求，但它不会像.aspx文件那样要返回处理结果和大量HTML，它可以返回简单的字符串、图片等。

大家知道，HttpHandle需要在Web.Config文件中进行相应的配置，那么，如果不配置呢？能否直接使用？可以的，来，动手玩玩。

1、在项目中添加一个.ashx文件，并改写默代码，如下：

```
using System;
using System.Web;

public class imageHandle : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "image/jpeg";

        string imgPath = @"~/images/{0}.jpg";
        //根据请求的查询字符串显示对应图片
        switch (context.Request.QueryString["id"])
        {
            case "1":
                context.Response.WriteFile(context.Server.MapPath(string.Format(imgPath, "1")));
                break;
            case "2":
                context.Response.WriteFile(context.Server.MapPath(string.Format(imgPath, "2")));
                break;
            case "3":
                context.Response.WriteFile(context.Server.MapPath(string.Format(imgPath, "3")));
                break;
            default:
                //默认显示第一张图片
                context.Response.WriteFile(context.Server.MapPath(string.Format(imgPath, "1")));
                break;
        }
    }
 
    public bool IsReusable {
        get {
            return true;
        }
    }

}
```

我简单说一下，这个类，根据请求的查询参数，返回对应的图片，所以，你可以随便准备3张图片做测试。

2、做好.aspx页面。

```
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div>
            <img id="img" 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">
    function GetImg(index) {
        var myImg = document.getElementById('img');
        myImg.src = './imageHandle.ashx?id=' + index;
    }
</script>
```

这个例子是这样的，有三个链接，分别为1、2、3，点击后显示对应的图片。

![](http://hi.csdn.net/attachment/201112/23/0_1324600468kvWs.gif)

.

我们在不配置Web.Config的情况下应用了HttpHandle，下面我配置一下Web.Config文件。

找到HttpHandlers节，并添加一个节点。

```
<add verb="*" path="loadimg" type="imageHandle" validate="false"/>
```

然后，把.aspx页面也改一下。

```
<script type="text/javascript">
    function GetImg(index) {
        var myImg = document.getElementById('img');
        myImg.src = './loadimg?id=' + index;
    }
</script>
```

这时候，神奇地发现无效了。咋回事呢？是不是没有把文件放进App\_Code的原因？

把.ashx文件扔到App\_Code目录下，结果发现，还是不行。

于是，我不用.ashx文件，直接新建一个.cs文件，把刚才.ashx里面的C#代码复制过去，并把类名改一下，接着运行。

这回成功了，是不是就说明，在Web.Config中配置的httpHandler对于.ashx中定义的类是无效的？