如何忽略MVC4 WebAPI配置中的路由?

Dee*_*101 5 asp.net-mvc asp.net-web-api asp.net-web-api-routing

我添加了elmah的MVC4项目.我的global.asax的Application_Start()有

WebApiConfig.Register(GlobalConfiguration.Configuration); // #1
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);   // #2
Run Code Online (Sandbox Code Playgroud)

#1和#2如下

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional } );
    }
    ...
}

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

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

模板是相同的,并且路由到控制器的工作完全符合我们的要求(从URI规范的角度来看).问题是在添加WebAPI路由之后添加了忽略路由.因此,MVC4s路由和由Elmah处理的内容(例如/elmah.axd/styles)被WebAPI拦截并且请求失败=>所以我的elmah.axd页面中没有CSS.我尝试在global.asax中翻转#1和#2,但这导致所有WebAPI路由失败 - FAR比不在Elmah中工作的CSS更糟糕!

我基本上需要一些方法来指示WebAPI的路由忽略{resource}.axd/{*pathInfo}正确作为第一条路径 - 我该怎么做?

Dee*_*101 8

这对我们有用 - 将忽略从包装中移出并作为第一个.

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        //ignore route first
        RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        // And taken out of the call below
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
Run Code Online (Sandbox Code Playgroud)