如何使MVC RPC样式API控制器使用多个POST方法

时间:2014-05-17 02:38:30

标签: c# api asp.net-mvc-4 rpc asp.net-apicontroller

我知道这个问题已在很多领域得到提及和回答,因为我已经搜索并看到了许多不同的答案和例子;但是,我无法应用任何这些并使其正常工作。

我正在尝试在MVC 4中组合一个RPC样式的apicontroller。在aipcontroller中,我有两种方法,我正在尝试执行发送和接收JSon:TestFunction1& TestFunction2。如果我注释掉其中任何一个,那么另一个工作正常。

当我尝试将它们都包括在内时,它会因以下(常见)错误而失败:

Multiple actions were found that match the request:
    TestFunction1 on type test.api.TestController
    TestFunction2 on type test.api.TestController"

尽我所知,我正在按照我发现的例子,我已经读过SO QnAs,直到我失明,我仍然无法弄清楚我做错了什么。

有人可以帮我修理并理解这个吗?

编辑:附加说明 - 如果我注释掉TestFunction2并且不更改我的javascript操作以匹配,我仍然会调用TestFunction1。

我的控制器方法:

    [HttpPost, ActionName("TestFunction1")]
    public HttpResponseMessage TestFunction1(SomeModel somemodel)
    {   SomeModel otherModel = new SomeModel() {
            id = 7,
            username = "other API-PostSomeModel Username",
            email = "other API-PostSomeModel email",
            password = "other API-PostSomeModel passowrd"
        };

        if (ModelState.IsValid) {
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, somemodel);
            var serializer = new JavaScriptSerializer();

            response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = somemodel.id }));
            response.Content = new StringContent(serializer.Serialize(otherModel));

            return response;
        }
        else {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
        }
    }

    [HttpPost, ActionName("TestFunction2")]
    public HttpResponseMessage TestFunction2(SomeModel somemodel)
    {   SomeModel otherModel = new SomeModel() {
            id = 7,
            username = "other API-TestFunction Username",
            email = "other API-TestFunction email",
            password = "other API-TestFunction passowrd"
        };

        var serializer = new JavaScriptSerializer();
        var resp = new HttpResponseMessage() {
            Content = new StringContent(serializer.Serialize(otherModel))
        };

        resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        return resp;
    }

模特:

public class SomeModel
{   [Key]
    public long id { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public string email { get; set; }

    public SomeModel() {}
    public SomeModel(long _id, string usrname, string pswd, string _email) {
       id = _id;
        username = usrname;
        password = pswd;
        email = _email;
    }   
}

我的路线:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //Route Catches the GET PUT DELETE typical REST based interactions (add more if needed)
        routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{id}",
            defaults: new {
                id = RouteParameter.Optional,
                httpMethod = new HttpMethodConstraint("GET", "PUT", "DELETE")
            }
        );

        //This allows POSTs to the RPC Style methods http://api/controller/action
        routes.MapHttpRoute(
            name: "API RPC Style",
            routeTemplate: "api/{controller}/{action}",
            defaults: new {
                httpMethod = new HttpMethodConstraint("POST"),  
            }
        );

        //Finally this allows POST to typical REST post address http://api/controller/
        routes.MapHttpRoute(
            name: "API Default 2",
            routeTemplate: "api/{controller}/{action}",
            defaults: new {
                httpMethod = new HttpMethodConstraint("POST"),  
            }
        );

        routes.MapRoute(
             name: "Default",
             url: "{controller}/{action}/{id}",
             defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

我的javascript:

 function jsTestFunction(id, username, password, email) {
    alert("Verify data:" + "\n\r" + id + "\n\r" + username + "\n\r" + password + "\n\r" + email);
    var obj = { 
        'id' : id,
        'username' : username,
        'password' : password,
        'email' : email,
    };
    var postdata = JSON.stringify(obj);
    $.ajax({
        type: "POST",
        url: "api/Test/TestFunction2/",
        dataType: "json",
        traditional: true,
        data: postdata,
        contentType: "application/json; charset=utf-8",
        success: function (responsedata) {
            var temp0 = responsedata['id'];
            var temp1 = responsedata['username'];
            var temp2 = responsedata['password'];
            var temp3 = responsedata['email'];
            if (!responsedata.error) {
                alert("The process was successful: " + "\n\r" + temp0 + "\n\r" + temp1 + "\n\r" + temp2 + "\n\r" + temp3);
            }
            else {
                alert("An error occured during the process: " + responsedata.msg);
                $('#reservation-result').html("Error :" + responsedata.msg).fadeIn(2000);
            }
        },
        error: function (error) {
            alert("There was an error posting the data to the server: " + error.responseText);
        }
    });
 }

1 个答案:

答案 0 :(得分:0)

我终于修好了。当我在注释掉TestFunction2并且在我在javascript中调用TestFunction2时仍然调用TestFunction1时,问题出现在路由中是相当明显的。

我还注意到我在RouteConfig.cs中有我的映射。这可能与问题无关;除了它应该在WebApiConfig中正确地执行它。在功能上,我认为它与它没有任何关系。

当我将路由“API RPC Style”的顺序与“API Default”交换时,最终修复的是什么。然后它立即开始工作。现在我可以以RPC方式执行API POST调用,并且它可以正常工作。

我不能说我完全理解路由机制;但是,我知道订单很重要。这是第一场比赛被选中,而不是最佳比赛。在这种情况下,它是有道理的;因此,我会用它来运行。