扩展HTTP管道

应用程序事件的管道不仅仅限于对.aspx Web表单的请求,他还可用于创建自己的处理程序来处理自定义的文件类型。

所有asp.net 应用程序的请求都在称为HTTP处理程序的特殊组件处理。HTTP处理程序是asp.net请求处理框架的骨架。

在web.config中的配置

<httpHandlers>
            <add verb="*" path="source.simple" type="SourceHandler"/>
            <add verb="*" path="test.simple" type="SimpleHandler"/>

            ....

如果你希望工作于web表单模型之外的低层来支持特殊格式的处理,可以实现自己的HTTP处理程序。

1.可以在App_Code目录创建一个实现IHttpHandler接口的类。

代码为:

using System;
using System.Web;

public class SimpleHandler : IHttpHandler
{

//ASP.NET获得请求会调用这个方法
    public void ProcessRequest(System.Web.HttpContext context)
    {
        HttpResponse response = context.Response;
        response.Write("<html><body><h1>Rendered by the SimpleHandler");
        response.Write("</h1></body></html>");
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

如果你在一个类库项目里创建这个扩展,需要添加对System.web.dll程序集的引用,它包含大量的ASP.NET类。

2.配置自定义的HTTP处理程序


在web.config文件中注册你的HTTP处理程序

<httpHandlers>
            <add verb="*" path="source.simple" type="SourceHandler"/>
            <add verb="*" path="test.simple" type="SimpleHandler"/>

</httpHandlers>

注册HTTP处理程序时,需要注意指定三个重要环节。verb特性表明请求是一个HTTP POST 还是HTTP GET请求(使用*表示所有的请求)。

path特性表明将要调用HTTP处理程序的文件的扩展名。

最后,type特性标识HTTP处理程序类。

用这种方式有缺点:当部署应用程序到IIS WEB服务器时,使用自定义的HTTP处理程序并不是很方便。

因为IIS5或IIS6不能识别.simple扩展或者没有意识到它与ASP.NET是关联的。相反,IIS只检查名为test.simple的文件。

要改变这种行为,你需要手工的去添加一个IIS映射。

3.在不配置IIS的情况下注册HTTP处理程序


ASP.NET还提供了另一个注册HTTP处理程序的方法-使用可识别的扩展名.ashx。所有以ashx结尾的请求自动识别为对自定义HTTP处理程序的请求。

.ashx文件以webHandle指令开头。下面的例子:

<%@ WebHandler Language="C#" class="SourceHandler" %>

最重要的是,.ashx文件类型已经在IIS里注册,所以在发布应用程序的时候不必执行任何IIS配置。

究竟是使用配置文件还是.ashx文件只是个人偏好问题。不过.ashx文件通常用于为单个Web应用程序设计的简单一些的扩展。

配置文件给你的灵活性更大一些

SourceHandler这个类在App_Code目录下

这个类的示例:

public class SourceHandler : IHttpHandler
{
    public void ProcessRequest(System.Web.HttpContext context)
    {
        // Make the HTTP context objects easily available.
        HttpResponse response = context.Response;
        HttpRequest request = context.Request;
        HttpServerUtility server = context.Server;

        response.Write("<html><body>");

        // Get the name of the requested file.
        string file = request.QueryString["file"];
        try
        {
            // Open the file and display its contents, one line at a time.
            response.Write("<b>Listing " + file + "</b><br>");
            StreamReader r = File.OpenText(server.MapPath(Path.Combine("./", file)));
            string line = "";
            while (line != null)
            {
                line = r.ReadLine();

                if (line != null)
                {
                    // Make sure tags and other special characters are
                    // replaced by their corresponding HTML entities, so they
                    // can be displayed appropriately.
                    line = server.HtmlEncode(line);

                    // Replace spaces and tabs with non-breaking spaces
                    // to preserve whitespace.                       
                    line = line.Replace(" ", "&nbsp;");
                    line = line.Replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");

                    // A more sophisticated source viewer might apply color-coding.
                    response.Write(line + "<br>");
                }
            }
            r.Close();
        }
        catch (ApplicationException err)
        {
            response.Write(err.Message);
        }
        response.Write("</html></body>");
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

这段代码找到请求的文件,读取他的内容,并使用一个字符串替换和HTML编码创建一个可以被浏览器安全显示的内容

然后,你还需要处理程序映射到文件扩展名,

<httpHandlers>
            <add verb="*" path="source.simple" type="SourceHandler"/>

<httpHandlers>

http://localhost:1390/Website/source.simple?file=HolmesQuote.aspx.cs

原文地址:https://www.cnblogs.com/gull/p/1878159.html