Rest ASP.NET Web Api路由无法捕获参数

Rest ASP.NET Web Api路由无法捕获参数,rest,routes,asp.net-mvc-routing,asp.net-web-api,input-parameters,Rest,Routes,Asp.net Mvc Routing,Asp.net Web Api,Input Parameters,我在global.asax.cs文件中设置了一些路由,如下所示: routes.MapHttpRoute( name: "ColorsRoute", routeTemplate: "api/{controller}/{action}/{styleId}", defaults: new { controller = "Cars", acti

我在global.asax.cs文件中设置了一些路由,如下所示:

routes.MapHttpRoute(
            name: "ColorsRoute",
            routeTemplate: "api/{controller}/{action}/{styleId}",
            defaults: new
            {
                controller = "Cars",
                action = "Colors"
            }
        );
localhost:58645/api/cars/colors/18752867
localhost:58645/api/cars/colors?styleid=18752867
控制员:

    public Dictionary<string, string> Colors(string styleid)
    {
        //returns a dictionary of colors and ids
    }
    public Dictionary<string, string> Makes(string state)       
    {
        //return dictionary of makes and ids
    }
不会拾取styleId,但传统上它是这样使用的:

routes.MapHttpRoute(
            name: "ColorsRoute",
            routeTemplate: "api/{controller}/{action}/{styleId}",
            defaults: new
            {
                controller = "Cars",
                action = "Colors"
            }
        );
localhost:58645/api/cars/colors/18752867
localhost:58645/api/cars/colors?styleid=18752867
你知道为什么会这样吗

更新:

这些路由在“domain.com/api/cars/makes/new”这样的调用中运行良好:

控制器:

    public Dictionary<string, string> Colors(string styleid)
    {
        //returns a dictionary of colors and ids
    }
    public Dictionary<string, string> Makes(string state)       
    {
        //return dictionary of makes and ids
    }

根据您定义路由的顺序,匹配的路由可能不是您希望将请求发送到的路由。尤其是你的YearsRoute和ModelsRoute。您有相同数量的管段,但未指定任何布线约束

尝试将MapHttpRoute作为第一个注册的路由,并添加正则表达式约束

routes.MapHttpRoute(
        name: "ColorsRoute",
        routeTemplate: "api/{controller}/{action}/{styleId}",
        defaults: new
        {
            controller = "Cars",
            action = "Colors"
        },
      constraints: new { styleId = @"\d+" }
    );
看看现在是否匹配

更新

另一个选项是专门为URI定义一个路由,该路由直接映射到您想要使用的ApicController和操作方法。这样,请求总是转到您希望它执行的操作

根据您定义路由的顺序,匹配的路由可能不是您希望将请求发送到的路由。尤其是你的YearsRoute和ModelsRoute。您有相同数量的管段,但未指定任何布线约束

尝试将MapHttpRoute作为第一个注册的路由,并添加正则表达式约束

routes.MapHttpRoute(
        name: "ColorsRoute",
        routeTemplate: "api/{controller}/{action}/{styleId}",
        defaults: new
        {
            controller = "Cars",
            action = "Colors"
        },
      constraints: new { styleId = @"\d+" }
    );
看看现在是否匹配

更新

另一个选项是专门为URI定义一个路由,该路由直接映射到您想要使用的ApicController和操作方法。这样,请求总是转到您希望它执行的操作

您说您有一些路由,但您只显示了一个。你的全球路线中还有其他路线吗?Asaxy你说你有一些路线,但你只展示了一条。在您的Global.AsaxIt中还有其他路由吗?原来MakesRoute与它冲突,所以我物理定义了使它工作的控制器和操作。如果你按照这些思路更新你的答案,我会把它排除在外。事实证明MakesRoute与它冲突,所以我物理上定义了使它工作的控制器和操作。如果你更新了你的答案,我会删除它。