开发者

Creating my first HttpHandler - doesn't work

开发者 https://www.devze.com 2023-03-28 16:19 出处:网络
I\'m hoping there is a simple solution to this. I\'m using MVC 3. I have two projects in my solution, a class library named MyApp.Domain, and an MVC 3 Web application named MyApp.WebUI.

I'm hoping there is a simple solution to this. I'm using MVC 3. I have two projects in my solution, a class library named MyApp.Domain, and an MVC 3 Web application named MyApp.WebUI.

In MyApp.Domain, I have this file:

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

        public void ProcessRequest(HttpContext context)
        {
  开发者_如何学运维          context.Response.Write("test");
        }
    }
}

In MyApp.WebUI, in the web.config in the root of the project, I have this:

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

But if I navigate to http://localhost:52233/test.testhandler, I get a 404 error. I imagine there's some sort of namespace issue, but I don't know how to fix it.

Anyone come across this issue?


Try to ignore your URL pattern in the routing. Add this before the default route in the global.asax:

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


I think the problem is that IIS is not routing your request to your application. Only certain defined file extensions (like .aspx or .ashx) are registered with IIS for routing to an ASP.NET application. If you change your web.config line to

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

and your request to

http://localhost:52233/testhandler.ashx

you might have more success. Note that I have added the assembly name into the 'type' value - I think that's required too.

0

精彩评论

暂无评论...
验证码 换一张
取 消