【发布时间】:2013-02-21 12:06:25
【问题描述】:
我正在尝试在一个控制器中实现一个具有多个 POST 方法的控制器。我有以下内容:
public class PatientController : ApiController
{
[HttpGet]
public IEnumerable<Patient> All() { ... }
[HttpGet]
public Patient ByIndex(int index) { ... }
[HttpPost]
public HttpResponseMessage Add([FromBody]Patient patient) { ... }
}
我的路由上有这个:
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
"API_1",
"{controller}/{index}",
new { index = RouteParameter.Optional });
一切都按预期工作:)
现在,我想添加以下操作:
[HttpPost, ActionName("save")]
public void Save(int not_used = -1) { ... }
在没有向路由添加任何内容的情况下,我在 Fiddler 中收到以下错误:找到与请求匹配的多个操作。
如果我将此添加到我的路由中(作为第二个或第一个,没关系):
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
"API_2",
"{controller}/{action}/{not_used}",
new { not_used = RouteParameter.Optional },
new { action = "save|reset" }); // Action must be either save or reset
我会在 Fiddler 中得到同样的错误。
这甚至可能吗?我可以在一个控制器中拥有多个具有不同(类型)参数的 POST 吗?
【问题讨论】:
-
你打电话的网址是什么?另外,你真的没有在 api URL 中使用普通的
/api前缀吗? -
是的,没有 api - 该项目根本不包含 MVC。当使用以下 POST localhost:6850/Patient/ 对 im 进行定位时
标签: c# asp.net rest asp.net-web-api