【发布时间】:2015-01-20 23:18:54
【问题描述】:
所以我有三个不同的 Get 操作:
public IHttpActionResult Get(){
//get all entries
}
public IHttpActionResult Get(int id){
//get an entry based on an ID
}
public IHttpActionResult Get(int page =0, int pageSize = 2){
//Pagination:get entries by pages of 2 entries
}
起初我使用的是前两种方法,使用此路由很好:
config.Routes.MapHttpRoute("Default",
"api/{controller}/{id}",
defaults: new { controller = "project", id = RouteParameter.Optional }
);
添加第三个有两个参数的 Get 操作后,它开始返回异常:找到与请求匹配的多个操作。顺便说一句,我将页面参数作为查询字符串发送,如下所示:api/project/?page=0。
我确实知道最后两个操作是问题的根源,路由器无法决定匹配哪一个,但我无法提出正确的路由功能。
虽然,我使用了属性路由来解决这个问题:Route["api/project/{page:int}/{pageSize:int}"] 我更感兴趣的是使用旧的路由方式来解决这个问题。
对于冗长的信息,我深表歉意,并提前感谢您的宝贵时间。
【问题讨论】:
标签: asp.net-mvc get routing asp.net-web-api asp.net-web-api2