1

我有一条路线:

routes.MapRoute (name: "apicontroller2",
                 url: "api/{controller}/{action}/",
                 defaults: new { controller = "Default2", action = "Index" }
                );

和具有两个 post 方法的 Default2Controller:

[HttpPost] 
public HttpResponseMessage Post(ttReview review)
{
...
}

[HttpPost] 
public HttpResponseMessage PostPro(ttbewertungenpro pro)
{
...
}

webserver/api/default2/PostPro/当我通过或webserver/api/default2/Post/ajax post调用这些 API-Methods 时,我得到了错误:

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");
        }
}

我的路线错了还是什么?当我删除一种方法时,另一种方法有效...

4

2 回答 2

2

查看AttributeRouting (http://nuget.org/packages/AttributeRouting.WebApi)

绘制路线要容易得多。

例子

[POST("Page/Method")]
public HttpResponseMessage Post(ttReview review)
{
...
}
于 2013-01-17T11:31:45.350 回答
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);
}

索引.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" />

这对我来说很好,所以问题一定是你的路径/路线。

于 2013-01-17T12:32:26.787 回答