我有一条路线:
routes.MapRoute (name: "apicontroller2",
url: "api/{controller}/{action}/",
defaults: new { controller = "Default2", action = "Index" }
);
和Default2Controller有两个post方法:
[HttpPost]
public HttpResponseMessage Post(ttReview review)
{
...
}
[HttpPost]
public HttpResponseMessage PostPro(ttbewertungenpro pro)
{
...
}
当我通过webserver/api/default2/PostPro/
或webserver/api/default2/Post/
ajax帖子调用这些API方法时,我收到错误:
ExceptionMessage=Multiple actions were found that match the request:
System.Net.Http.HttpResponseMessage Post(WT.Models.ttReview) on type WT.Controllers.Default2Controller
System.Net.Http.HttpResponseMessage PostPro(WT.Models.ttbewertungenpro) on type WT.Controllers.Default2Controller
我的ajax电话是:
$.ajax({
url: "../api/default2/Post", // or "../api/default2/PostPro",
type: 'POST',
dataType: "text",
data: review, // or pro
success: function (test) {
},
error: function (test) {
alert("Error");
}
}
我的路线错了还是什么? 当我删除一种方法时,另一种方法有效...
答案 0 :(得分:2)
查看 AttributeRouting (http://nuget.org/packages/AttributeRouting.WebApi)
映射路线要容易得多。
实施例
[POST("Page/Method")]
public HttpResponseMessage Post(ttReview review)
{
...
}
答案 1 :(得分:1)
使用全新的空白WebAPI项目 - 我没有触及global.asax文件。
家庭控制器
[HttpPost]
public HttpResponseMessage Post(string review)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}
[HttpPost]
public HttpResponseMessage PostPro(string pro)
{
return new HttpResponseMessage(HttpStatusCode.Created);
}
Index.cshtml
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script>
$(document).ready(function () {
$("#submit").click(function() {
$.ajax({
url: "home/Post", // or "../api/default2/PostPro",
type: 'POST',
dataType: "text",
data: "test", // or pro
success: function(test) {
},
error: function(test) {
alert("Error");
}
});
});
});
</script>
<input type="submit" value="submit" id="submit" />
这对我来说很好,所以问题必须是你的路径/路线。