如何绕过例外的ASP.NET Web API中发现多个操作 [英] How to bypass the exception multiple actions were found in ASP.NET Web API
问题描述
在试图找到这个问题如下解决方案:
When trying to find solution for this below question:
<一个href=\"http://stackoverflow.com/questions/11724749/mvc-web-api-route-with-default-action-not-working/11725968#11725968\">MVC网页API路由默认动作不灵
主要是我在整个运行问题多个动作被发现。如果路由机制发现与路由匹配的多个动作,就抛出异常500。
Mostly I run across the problem "multiple actions were found". If routing mechanism finds out more than one actions matched with a route, it throws out the exception 500.
例如,有两个动作中ValuesController:
For example, there are two actions in ValuesController:
[HttpGet]
public IEnumerable<String> Active()
{
var result = new List<string> { "active1", "active2" };
return result;
}
public IEnumerable<string> Get()
{
return new[] { "value1", "value2" };
}
该比赛与默认路由:
which match with default route:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
通过
GET: /api/values
会被发现的错误多个动作。
will get error multiple actions were found.
我的问题:我怎么能选择指定的动作绕过异常多个动作被发现(选择第一个匹配的动作也没关系)
My question: how I can bypass the exception "multiple actions were found" by choosing specified action (choose the first matched action is also okay).
更新1:正在从tugberk的评论更新,这要归功于指出
Update 1: Update from tugberk's comment, thanks to point out.
更新2:正在从迈克的评论更新,似乎这个问题是不正确的多,所以我改变由不提路由约束问路
Update 2: Update from Mike's comment, seems the question is not correct much, so I change the way to ask by not mentioning the routing constraint.
感谢
推荐答案
首先,它不应该是可能的,除非你申请HTTPGET它属性的路线,以配合积极的行动。我会假设你已经做到了。第二件事是,你想要的是不符合逻辑的。你想作一个GET请求,并有两个相同的动作。您现在正在使用基于HTTP的方法,所谓的路由。对于你的情况,我相信所谓的基于动作的路由是最好的。
First of all, it shouldn't be possible for those route to match Active action unless you apply HttpGet attribute on it. I will assume you already did that. The second thing is that what you want is not logical. You would like to make a get request and have two identical actions. You are now using so-called HTTP-method-based routing. For you situation, I am sure that so-called action-based routing is the best.
假设你有以下两个动作:
Assuming you have following two actions:
[HttpGet]
public IEnumerable<String> Active()
{
var result = new List<string> { "active1", "active2" };
return result;
}
[HttpGet]
public IEnumerable<string> Retrieve()
{
return new[] { "value1", "value2" };
}
您路线应该如下:
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
然后,你可以要求那些具有下列的URI:
Then you can request to those with following URIs:
GET / controllername /检索
GET /controllername/retrieve
GET / controllername /主动
GET /controllername/active
这篇关于如何绕过例外的ASP.NET Web API中发现多个操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!