HTTP Handler not being invoked

孤街浪徒 提交于 2019-12-12 01:29:19

问题


I have a custom HTTP Handler that properly implements IHttpHandler. Below is what I've configured in my webConfig. If I understand correctly this block should capture any requests with .test as the extension.

<handlers> 
    <add name="SampleHandler" verb="*" path="*.test" 
     type="TestApp.App_Start.CustomHandler, TestApp" />
</handlers>

However, the only time this handler is ever invoked is when I have a path depth of three applied to the request URL. All other requests will 404.

For instance the handler works correctly when the path is:

localhost:XXX\some\fake\path\file.test

But not for:

localhost:XXX\some\file.test

I am using ASP.NET MVC 5, and suspect it has something to do with the routing. I also am using the sample project provided in VS2013 so besides a handler being added to the project I really haven't done anything else.

Update:

I've determined the default route is interfering.

  routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

Is there way to get it working even with this route configured?


回答1:


It looks like the routing was interfering with the handler. In order to allow the handler to recieve the request I needed to call IgnoreRoute method against the current RouteCollection ignoring any routes with .test in them:

Inside the RouteConfig class

  public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{resource}.test/");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }

It works now. Is there a better way to do this?




回答2:


I believe the reason for you HTTP Handler not being called is because you are registering in web apps for IIS 6. For IIS 7 and above, do it like this:

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="*" path="SampleHandler" 
        type="TestApp.App_Start.CustomHandler, TestApp" />
    </httpHandlers>
  </system.web>
</configuration>


来源:https://stackoverflow.com/questions/24877013/http-handler-not-being-invoked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!