在控制器中,我有一个这样的方法:
public class FooController : ApiController
{
[HttpPost]
public HttpResponseMessage ApplySomething(int id)
{
...
}
}
当我执行.../Foo/ApplySomething/
等POST请求并将id=1
作为POST值传递时,出现错误:
{
Message: "No HTTP resource was found that matches the request URI '.../Foo/ApplySomething/'."
MessageDetail: "No action was found on the controller 'Foo' that matches the name 'ApplySomething'."
}
但是当我更改URL以获得ID(如.../Foo/ApplySomething/1
)时,它可以正常工作,但是从URL获取值,而不是从POST值获取。
我做错了什么?
答案 0 :(得分:1)
默认情况下,Web API使用以下规则绑定参数:
根据这些规则,如果要从POST主体绑定参数,只需在类型前面添加[FromBody]
属性:
public HttpResponseMessage ApplySomething([FromBody] int id) { ... }
了解更多信息please see the documentation。
答案 1 :(得分:0)
试试这个:
public class FooController : ApiController
{
[HttpGet]
public HttpResponseMessage ApplySomething(int? id=0)
{
...
return View();
}
[HttpPost]
public HttpResponseMessage ApplySomething(FormCollection Form,int? id=0)
{
...
return View();
}
}
现在试试这个网址.../Foo/ApplySomething?id=1
希望它有效......!
答案 2 :(得分:0)
提及具有相同操作的httpget方法或 检查你的routeconfig文件。
您还可以参考以下解决方案:
解决方案1:No HTTP resource was found that matches the request URI in Web API
解决方案2:http://forums.asp.net/t/1916587.aspx?WebAPI+No+HTTP+resource+was+found+that+matches+the+request+URI