Multiple actions were found that match the request in Web Api
Answer1
Your route map is probably something like this:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
But in order to have multiple actions with the same http method you need to provide webapi with more information via the route like so:
routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
Notice that the routeTemplate now includes an action. Lots more info here: http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
Update:
Alright, now that I think I understand what you are after here is another take at this:
Perhaps you don't need the action url parameter and should describe the contents that you are after in another way. Since you are saying that the methods are returning data from the same entity then just let the parameters do the describing for you.
For example your two methods could be turned into:
public HttpResponseMessage Get()
{
return null;
}
public HttpResponseMessage Get(MyVm vm)
{
return null;
}
What kind of data are you passing in the MyVm object? If you are able to just pass variables through the URI, I would suggest going that route. Otherwise, you'll need to send the object in the body of the request and that isn't very HTTP of you when doing a GET (it works though, just use [FromBody] infront of MyVm).
Hopefully this illustrates that you can have multiple GET methods in a single controller without using the action name or even the [HttpGet] attribute.
Answer2
Update as of Web API 2.
With this API config in your WebApiConfig.cs file:
public static void Register(HttpConfiguration config)
{
//// Web API routes
config.MapHttpAttributeRoutes(); //Don't miss this
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional }
);
}
You can route our controller like this:
[Route("api/ControllerName/Summary")]
[HttpGet]
public HttpResponseMessage Summary(MyVm vm)
{
rturn null;
}
[Route("api/ControllerName/FullDetails")]
[HttpGet]
public HttpResponseMessage FullDetails()
{
return null;
}
Where ControllerName is the name of your controller (without "controller"). This will allow you to get each action with the route detailed above.
For further reading: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
Answer3
After a lot of searching the web and trying to find the most suitable form for routing map if have found the following
config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id =RouteParameter.Optional }, new { id = @"\d+" });
config.Routes.MapHttpRoute("DefaultApiWithAction", "Api/{controller}/{action}");
These mapping applying to both action name mapping and basic http convention (GET,POST,PUT,DELETE)
For me this worked, but only after changing the order of routes in the route configuration so that the one with action appeared first
https://www.cnblogs.com/chucklu/p/10283664.html
目前的修改方案是,添加了action
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
这样会导致的问题是,之前可以工作的,现在不工作了
request1 404错误
GET http://localhost/Chuck_WebApi/api/products HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0
request2 404错误
GET http://localhost/Chuck_WebApi/api/product/1 HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0
request1需要修改为
GET http://localhost/Chuck_WebApi/api/products/getallproducts HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0
request2需要修改为
GET http://localhost/Chuck_WebApi/api/products/getproduct/1 HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 0
Multiple actions were found that match the request in Web Api的更多相关文章
- Multiple actions were found that match the request Web API
在WebAPI工程入口不对外公开的接口不能使用public. [HttpPost] public string PostRequest([FromBody] Model model) { /// } ...
- ASP.NET MVC报错: Multiple types were found that match the controller named
当使用ASP.NET MVC的Area功能时,出现了这样的错误: Multiple types were found that match the controller named 'Home'. T ...
- Multiple types were found that match the controller named 'Auth'.
偶然的一个机会,修改了已经开发完毕的一个项目的命名.突然运行发现: { "Message": "An error has occurred.", "E ...
- [React + Functional Programming ADT] Create Redux Middleware to Dispatch Multiple Actions
We only have a few dispatching functions that need to be known by our React Application. Each one ac ...
- Can't bind multiple parameters ('header' and 'parameters') to the request's content.
2019-01-23 15:46:29.012+08:00 ERROR [6]: System.InvalidOperationException: Can't bind multiple param ...
- Request Entity Too Large for Self Hosted ASP.Net Web API在Selfhost的api后台怎么解决Request Entity Too Large问题
Request Entity Too Large for Self Hosted ASP.Net Web API在Selfhost的api后台怎么解决Request Entity Too Large问 ...
- request对象常用API 获取请求参数的值 request应用 MVC设计模式
1 request对象常用API 1)表示web浏览器向web服务端的请求 2)url表示访问web应用的完整路径:http://localhost:8080/day06/Demo1 ...
- [React Testing] The Redux Store - Multiple Actions
When using Redux, we can test that our application state changes are working by testing that dispatc ...
- MVC 区域内默认控制器不能访问(Multiple types were found that match the controller named ‘Index')
异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 错误信息 和主页的默认控制器冲突了,修改下Areas里面的默认控制器就可以了
随机推荐
- 面试心得与总结—BAT、网易、蘑菇街 - ImportNew
1. 九种基本数据类型的大小,以及他们的封装类. 2. Switch能否用string做参数? 3. equals与==的区别. 4. Object有哪些公用方法? 5. Java的四种引用,强弱软虚 ...
- c++ wchar_t
·C语言相关 对应于char, C语言中也有宽字符内型wchar_t.wchar_t被定义为: typedef unsigned short wchar_t ;显然它是16位的.wchar_t类型的常 ...
- 在Web工程中,普通Java类如何读文件
我们在以前讨论过在Web工程不能采用Java工程原先的读取方式,即创建一个文件流(FileInputStream)并给出一个文件目录(从src开始找一直找到你要读取文件的目录),这种的方式是不可行的. ...
- BLOCK方式实现OC程序中多个页面判定用户是否登录
在程序中经常会遇到这种情况,用户刚进入我们软件的时候我们是无需要求用户登录的,但是在下面的页面中,例如收藏,购买等页面的时候,显然在多个页面需要多次判定用户是否登录.试着用block简单的实现了一下该 ...
- 第14章—数据库连接池(C3P0)
spring boot 系列学习记录:http://www.cnblogs.com/jinxiaohang/p/8111057.html 码云源码地址:https://gitee.com/jinxia ...
- CCCallFunc, CCCallFuncN, CCCallFuncND 三者的区别
今天学习过程中,自己敲了一个例子,结果在执行Action的时候出现了错误.经排查发现是CCCallFunc使用的问题,应该使用CCCallFuncN,然后搜了下他们的区别,才知道,是因为有一个参数的问 ...
- 2015-03-18——mongodb的简单配置
参考网址:http://www.cnblogs.com/mecity/archive/2011/06/11/2078527.html#3060056 mongod 数据库启动程序 mongo 数据库操 ...
- Selenium定位不到指定元素原因之iframe(unable to locate element)
浏览过程中,图片中的内容可能太小,无法看清,可以>右键>在新标签中打开 Outline 项目原因,需要用selenium实现模拟登陆.模拟上传文件,自然就需要模拟点击[上传]按钮: 模拟点 ...
- 这些Python骚操作,你知道吗?
0x00 世界,你好 程序员第一次接触语言或者框架,基本上都有个 Hello World 的例子,这里 Python 直接将它做成了一个包. 0x01 Python 哲学 Python 执行 i ...
- Hessian矩阵与多元函数极值
Hessian矩阵与多元函数极值 海塞矩阵(Hessian Matrix),又译作海森矩阵,是一个多元函数的二阶偏导数构成的方阵.虽然它是一个具有悠久历史的数学成果.可是在机器学习和图像处理(比如SI ...