我在 global.asax.cs 文件中有一些路由设置,如下所示:
routes.MapHttpRoute(
name: "ColorsRoute",
routeTemplate: "api/{controller}/{action}/{styleId}",
defaults: new
{
controller = "Cars",
action = "Colors"
}
);
控制器:
public Dictionary<string, string> Colors(string styleid)
{
//returns a dictionary of colors and ids
}
问题是当使用这样的网址时:
localhost:58645/api/cars/colors/18752867
不选择 styleId,但它通常像这样使用它:
localhost:58645/api/cars/colors?styleid=18752867
关于为什么会发生这种情况的任何想法?
更新:
这些路由可以很好地处理像“domain.com/api/cars/makes/new”这样的调用:
routes.MapHttpRoute(
name: "MakesRoute",
routeTemplate: "api/{controller}/{action}/{state}",
defaults: new
{
controller = "Cars",
action = "Makes"
}
);
控制器:
public Dictionary<string, string> Makes(string state)
{
//return dictionary of makes and ids
}
以下是我拥有的其他路线:
routes.MapHttpRoute(
name: "ModelsRoute",
routeTemplate: "api/{controller}/{action}/{make}/{state}",
defaults: new
{
controller = "Cars",
action = "Models"
}
);
routes.MapHttpRoute(
name: "YearsRoute",
routeTemplate: "api/{controller}/{action}/{modelId}/{state}",
defaults: new {
controller = "Cars",
action = "Years"
}
);
routes.MapHttpRoute(
name: "StylesRoute",
routeTemplate: "api/{controller}/{action}/{make}/{model}/{year}",
defaults: new {
controller = "Cars",
action = "Styles"
}
);