【发布时间】:2012-09-24 07:36:29
【问题描述】:
在我的控制器UserApiController中,我有以下功能:
public object GetUsers(string CountryID, string StateID)
{
//biz
}
public object GetPositions(int CompanyID, int DepartmentID)
{
//biz
}
在我的控制器SalesApiController中,我有以下功能:
public object GetOrders(string CountryID, int CompanyID)
{
//biz
}
public object GetProducts(string CountrID, string StateID, int CompanyID)
{
//biz
}
在 web api 配置中,我可以这样映射:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{CountryID}/{StateID}",
defaults: new { }
它适用于 UserApiController.GetUsers,因为函数签名仅与 GetUsers 匹配。
现在,问题:
1.如何定义路由以处理具有相同参数数量的不同功能(在相同或不同控制器内)
2.如何定义路由以处理具有不同参数数量的不同功能(如果可能,在相同或不同控制器中)
【问题讨论】:
-
既然是webapi,何必担心路由参数呢?为什么不直接使用普通的旧查询字符串参数?
-
是也不是,我们可以使用老派的查询字符串,但是,最好坚持休息,因为这是 web api 的本质:)
标签: asp.net-mvc routes asp.net-web-api