我正在尝试解决如何为以下Web API控制器进行路由:
public class MyController : ApiController
{
// POST api/MyController/GetAllRows/userName/tableName
[HttpPost]
public List<MyRows> GetAllRows(string userName, string tableName)
{
...
}
// POST api/MyController/GetRowsOfType/userName/tableName/rowType
[HttpPost]
public List<MyRows> GetRowsOfType(string userName, string tableName, string rowType)
{
...
}
}
目前,我对URL使用以下路由:
routes.MapHttpRoute("AllRows", "api/{controller}/{action}/{userName}/{tableName}",
new
{
userName= UrlParameter.Optional,
tableName = UrlParameter.Optional
});
routes.MapHttpRoute("RowsByType", "api/{controller}/{action}/{userName}/{tableName}/{rowType}",
new
{
userName= UrlParameter.Optional,
tableName = UrlParameter.Optional,
rowType= UrlParameter.Optional
});
但目前只有第一种方法(有两个参数)有效。我是不是走对了行,或者我的URL格式或路由完全错了?路由对我来说就像是黑魔法。
发布于 2014-01-15 22:43:18
问题是您的api/MyController/GetRowsOfType/userName/tableName/rowType
地址总是匹配第一个路由,因此永远不会到达第二个路由。
简单的解决方法,首先注册你的RowsByType
路由。
https://stackoverflow.com/questions/21140305
复制相似问题