HttpHandler 101 失败

4
当我添加一个HTTP处理程序时:
<add verb="*" path="*test.aspx" type="Handler"/>

用类实现:
using System;
using System.Web;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable
    {
        get { return false; }
    }

}

当我尝试访问http://localhost:port/mysite/this-is-a-test.aspx时,我的ASP.NET应用程序会出现错误“无法加载类型'Handler'。”
我以为可能是命名空间的问题,所以我尝试了下面的内容,但仍然出现了相同的“无法加载类型'Test.Handler'”错误。
<add verb="*" path="*test.aspx" type="Test.Handler, Test"/>

使用类:

using System;
using System.Web;

namespace Test
{

    public class Handler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Write("Hello World");
        }

        public bool IsReusable
        {
            get { return false; }
        }

    }

}

我知道自己在ASP.NET方面已经有些生疏了,但是这个问题我一点头绪都没有。

3个回答

12

我猜您正在使用网站项目而不是Web应用程序项目。在这种情况下,您需要将处理程序(Handler.cs)的代码后置文件放置在特殊的App_Code文件夹中。标记文件(Handler.ashx)可以位于网站根目录下:

<%@ WebHandler Language="C#" Class="Handler" CodeBehind="Handler.cs" %>

那么你可以直接在web.config中声明你的处理程序:

<add verb="*" path="*test.aspx" type="Handler"/>

0

默认情况下,asp.net Pagerhandlerfactory对象将处理所有的.aspx资源请求。


0

当处理程序是我App_Code目录中的一个类时,以下内容适用于我:

    <add verb="*" path="*test.aspx" type="Test.Handler,__Code"/>

我只为整个前缀(如“*.test”)添加了处理程序。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接