2011-08-17 34 views
2

我希望有一个简单的解决方案。我正在使用MVC 3.我的解决方案中有两个项目,名为MyApp.Domain的类库和名为MyApp.WebUI的MVC 3 Web应用程序。创建我的第一个HttpHandler - 不起作用

在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错误。我想有一些名称空间问题,但我不知道如何解决它。

任何人都遇到过这个问题?

回答

3

尝试忽略路由中的URL模式。在global.asax中的默认路由之前添加此项:

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

好的,赶上,谢谢。 – Steven

+0

帮助我呢!谢谢! –

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

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

相关问题