[英]MVC4 WebApi Routing with ActionNames not allowing for generic “Get”
I've been having trouble trying to be able to have multiple "Get" methods while also having a default of /api/{controller}. 我一直无法尝试拥有多个“获取”方法,同时还具有默认的/ api / {controller}。 Here is an example with dummy code (yes I realize it would return the exact same). 这是一个带有伪代码的示例(是的,我知道它将返回完全相同的代码)。
Error: "Multiple actions were found that match the request"
when I try to go to /api/courses
错误:当我尝试转到/api/courses
时"Multiple actions were found that match the request"
Going to /api/courses/all
works fine and so does /api/courses/3
转到/api/courses/all
可以, /api/courses/3
// GET api/courses
public IEnumerable<Courses> Get()
{
return Db.Courses.OrderBy(x => x.Name);
}
// GET api/courses/all
[ActionName("all")]
public IEnumerable<Courses> GetAll()
{
return Db.Courses.OrderBy(x => x.Name);
}
// GET api/courses/id
[ActionName("all")]
public Courses Get(int id)
{
return Db.Courses.Where(x => x.id == id);
}
RouteConfig
looks like: RouteConfig
看起来像:
routes.MapHttpRoute(
"ApiControllerOnly",
"api/{controller}"
);
// Allow for numeric Ids to be passed in
routes.MapHttpRoute(
"ApiControllerAndIntegerId",
"api/{controller}/{id}",
null,
new { id = @"^\d+$" }
);
routes.MapHttpRoute(
"ApiControllerAction",
"api/{controller}/{action}"
);
What I want to be able to do is call: 我想要做的是致电:
/api/courses - returns Get()
/api/courses/all - returns GetAll()
/api/courses/3 - returns Get(id = 3)
Edit: 编辑:
It also needs to allow for Put, Post, etc to work as well such as Putting
a course on /api/courses/ 它还需要允许Put,Post等正常工作,例如Putting
课程放在/ api / courses /
Error: "Multiple actions were found that match the request" when I try to go to /api/courses 错误:当我尝试转到/ api / courses时,“发现多个符合要求的动作”
By specifying the default action to be "Get" for the ApiControllerOnly
route, I was able to get /api/courses
to work. 通过将ApiControllerOnly
路由的默认操作指定为“ Get”,我可以使/api/courses
正常工作。 Does this work for your scenario? 这对您的情况有用吗?
routes.MapHttpRoute(
name: "ApiControllerOnly",
routeTemplate: "api/{controller}",
defaults: new { action = "Get" }
);
If you have additional Put/Post/etc methods in you APIController, you should consider using HttpMethodConstraint
on the ApiControllerOnly
route and add another route (ie ApiControllerOnly2
) for the Put/Post/etc methods: 如果APIController中还有其他Put / Post / etc方法,则应考虑在ApiControllerOnly
路由上使用HttpMethodConstraint
并为Put / Post / etc方法添加另一个路由(即ApiControllerOnly2
):
config.Routes.MapHttpRoute(
name: "ApiControllerOnly",
routeTemplate: "api/{controller}",
defaults: new { action = @"Get" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
config.Routes.MapHttpRoute(
name: "ApiControllerOnly2",
routeTemplate: "api/{controller}"
);
config.Routes.MapHttpRoute(
name: "ApiControllerAndIntegerId",
routeTemplate: "api/{controller}/{id}",
defaults: null,
constraints: new { id = @"^\d+$" }
);
config.Routes.MapHttpRoute(
name: "ApiControllerAction",
routeTemplate: "api/{controller}/{action}"
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.