创建我的第一个HttpHandler - 不起作用

时间:2011-08-17 21:24:26

标签: c# asp.net asp.net-mvc asp.net-mvc-3

我希望有一个简单的解决方案。我正在使用MVC 3.我的解决方案中有两个项目,一个名为MyApp.Domain的类库,以及一个名为MyApp.WebUI的MVC 3 Web应用程序。

在MyApp.Domain中,我有这个文件:

namespace MyApp.Domain.Test
{
    public class MyHandler : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write("test");
        }
    }
}

在MyApp.WebUI中,在项目根目录的web.config中,我有:

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="*.testhandler" type="MyApp.Domain.Test.MyHandler"/>
    </httpHandlers>
  </system.web>
</configuration>

但如果我导航到http://localhost:52233/test.testhandler,我会收到404错误。我想有一些命名空间问题,但我不知道如何解决它。

有人遇到过这个问题吗?

2 个答案:

答案 0 :(得分:3)

尝试忽略路由中的网址格式。在global.asax中的默认路由之前添加:

routes.IgnoreRoute("{resource}.testhandler/{*pathInfo}");

答案 1 :(得分:0)

我认为问题在于IIS没有将您的请求路由到您的应用程序。只有某些已定义的文件扩展名(如.aspx或.ashx)在IIS中注册,以便路由到ASP.NET应用程序。如果将web.config行更改为

<add verb="*" path="testhandler.ashx" type="MyApp.Domain.Test.MyHandler,MyAssembly"/>

以及您的

请求

http://localhost:52233/testhandler.ashx

你可能会取得更大的成功。请注意,我已将程序集名称添加到“类型”值中 - 我认为这也是必需的。

相关问题