这些Web API路由的顺序有什么问题

时间:2018-02-02 20:29:14

标签: c# asp.net-web-api routes asp.net-web-api-routing

所以我在WebApiConfig.cs文件中添加的路线很少,我可以获得第一个&第二次工作,但无法获得GADailyAPI工作。我得到一个404.当代码第一次写入时,前两行路线位于底部,它们将抛出404.我尝试改变顺序,但我不能让它们全部工作。有任何想法吗? 注意:这不是我的代码,所以如果可以的话,我不想修改大部分API代码。如果我能找出路由顺序并修复它将是最佳解决方案

        config.Routes.MapHttpRoute(
            name: "GAMonthlyApiNetworkStats",
            routeTemplate: "api/{controller}/{action}/{Year}/{Month}",
            defaults: new { controller = "GAMonthlyAPI", action = "NetworkStats" }
        );

        config.Routes.MapHttpRoute(
            name: "GAMonthlyApiAllStats",
            routeTemplate: "api/{controller}/{action}/{Year}/{Month}",
            defaults: new { controller = "GAMonthlyAPI", action = "AllStats", }
        );

        config.Routes.MapHttpRoute(
            name: "GADailyApiNetworkStats",
            routeTemplate: "api/{controller}/{action}/{StartDate}/{EndDate}",
            defaults: new { controller = "GADailyAPI", action = "NetworkStats" }
        );

        config.Routes.MapHttpRoute(
            name: "GADailyApiAllStats",
            routeTemplate: "api/{controller}/{action}/{StartDate}/{EndDate}",
            defaults: new { controller = "GADailyAPI", action = "AllStats" }
        );

        config.Routes.MapHttpRoute(
            name: "GAMonthlyApiGroupStats",
            routeTemplate: "api/{controller}/{action}/{Year}/{Month}/{GroupID}",
            defaults: new { controller = "GAMonthlyAPI", action = "GroupStats" }
        );
        config.Routes.MapHttpRoute(
            name: "GAMonthlyApiSiteStats",
            routeTemplate: "api/{controller}/{action}/{Year}/{Month}/{SiteID}",
            defaults: new { controller = "GAMonthlyAPI", action = "SiteStats" }
        );

        config.Routes.MapHttpRoute(
            name: "GADailyApiGroupStats",
            routeTemplate: "api/{controller}/{action}/{StartDate}/{EndDate}/{GroupID}",
            defaults: new { controller = "GADailyAPI", action = "GroupStats" }
        );
        config.Routes.MapHttpRoute(
            name: "GADailyApiSiteStats",
            routeTemplate: "api/{controller}/{action}/{StartDate}/{EndDate}/{SiteID}",
            defaults: new { controller = "GADailyAPI", action = "SiteStats" }
        );

2 个答案:

答案 0 :(得分:3)

订单没有任何问题,因为第一场比赛总是胜利,所以无论你选择以/api开头的5个段的路线,只会到达第一条路线。这是因为像{action}这样的占位符字面上可以是任何字符串。所以你基本上是在说:

Match if the url is: /api/<anything>/<anything>/<anything>/<anything>

在所有前4条路线中:

Match if the url is: /api/<anything>/<anything>/<anything>/<anything>/<anything>

其余的。

要解决此问题,您需要以某种方式约束路由,以便路由框架在不匹配时跳过第一个路由,然后转到下一个路由并尝试匹配该路由。

    config.Routes.MapHttpRoute(
        name: "GAMonthlyApiNetworkStats",
        routeTemplate: "api/{controller}/NetworkStats/{Year}/{Month}",
        defaults: new { controller = "GAMonthlyAPI", action = "NetworkStats" }
    );

    config.Routes.MapHttpRoute(
        name: "GAMonthlyApiAllStats",
        routeTemplate: "api/{controller}/AllStats/{Year}/{Month}",
        defaults: new { controller = "GAMonthlyAPI", action = "AllStats", }
    );

现在,/api/GAMonthlyAPI/AllStats/2012/09等网址会跳过第一条路线,因为AllStats段与第3段不匹配,并且会匹配第二条路线,因为第3段与第3段相同价值已经过去了。

参考:Why map special routes first before common routes in asp.net mvc?

答案 1 :(得分:0)

尝试添加httpMethod和约束,它可能会有所帮助:

routes.MapHttpRoute(
name: "ApiPut", 
routeTemplate: "api/{controller}/{id}",
defaults: new { action = "Put" }, 
constraints: new { httpMethod = new HttpMethodConstraint("Put") }
);