Asp.net web api 方法调用GET而不是POST

Asp.net web api 方法调用GET而不是POST,asp.net-web-api,asp.net-web-api-routing,Asp.net Web Api,Asp.net Web Api Routing,我有这种“POST”方法 [HttpPost] [System.Web.Mvc.ValidateAntiForgeryToken] public HttpResponseMessage UpdateProfile(string sAppUser) { MobileProfileModel profileModel= JsonConvert.DeserializeObject<MobileProfileModel>(sAppUser); using (ucApp = n

我有这种“POST”方法

[HttpPost]
[System.Web.Mvc.ValidateAntiForgeryToken]
public HttpResponseMessage UpdateProfile(string sAppUser)
{
    MobileProfileModel profileModel= JsonConvert.DeserializeObject<MobileProfileModel>(sAppUser);
    using (ucApp = new UserControllerApplication())
    {
        //this code should match the
        bool success = ucApp.UpdateUserProfile(profileModel);
        var response = Request.CreateResponse<bool>(HttpStatusCode.Created, success);

        string uri = Url.Link("DefaultApi", new { result = success });
        response.Headers.Location = new Uri(uri);
        return response;
    }
}
我得到了这个错误

<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>

我正在同一个控制器上执行一个Get方法,它可以工作,这实际上是初始Get的post。

我认为有两个原因导致此错误消息:

服务器uri路由将请求路由到错误的方法 您的ajax请求调用了错误的uri 如果您想确保呼叫POST,请通过wireshark查看,或尝试其他POST工具,例如curl

更新

您的uri表单AJAX请求是:

/api/User/UpdateProfile
您的uri模板是:

api/{controller}/{action}/{id}
我想你会犯这个错误,因为它们不匹配

顺便说一下:您不应该使用post更新配置文件,请使用带有ID的PUT

例如:

$.ajax({
            url: "/api/User/UpdateProfile/"+UserID,
            data:JSON.stringify(profile),
            type: 'PUT',
            contentType: 'application/json',
            //dataType: "json",
            async: false,
            cache: false,
            success: function (data) {
                 $.blockUI({ message: "Success" });
            },
            error: function (xhr) {
                alert(xhr.responseText);
            },  
               beforeSend: function() {
                   $.blockUI({ message: $("#ajaxLoader") });
               },
               complete: function() {
                   $.unblockUI();
               }
        });
在控制器中:

[HttpPut]
    [System.Web.Mvc.ValidateAntiForgeryToken]
    public HttpResponseMessage UpdateProfile(string sAppUser) // I don't know how to parse the id from uri ;) 
    {
        //find the user by his uniqe ID
        MobileProfileModel profileModel= JsonConvert.DeserializeObject<MobileProfileModel>(sAppUser);
        using (ucApp = new UserControllerApplication())
        {
            //this code should match the
            bool success = ucApp.UpdateUserProfile(profileModel);
            var response = Request.CreateResponse<bool>(HttpStatusCode.Created, success);

            string uri = Url.Link("DefaultApi", new { result = success });
            response.Headers.Location = new Uri(uri);
            return response;
        }
    }

这里有两件事,第一,为什么在ajax请求中使用完整路径而不是绝对路径?这只是一个例子吗?我会向你推荐绝对路径

接下来,如果您可以在ajax调用中尝试此方法,请更改以下内容

url: "http://mydomain.com/api/User/UpdateProfile",

或者,如果您想在ajax调用中使用data:属性,请如下所示

data: {sAppUser: profile},

如果这没有帮助,请告诉我。

添加了我当前的路由定义,这些定义在系统中的其他任何地方都有效。您是否正在从同一个域到同一个域进行ajax调用?有解决方案吗?所有浏览器上都有吗?我只在Chrome上看到这一点……这只发生在Chrome上。我不记得这个问题是否解决了,因为时间太长了,完整路径的原因是呼叫是从一个完全不同的站点进行的
url: "http://mydomain.com/api/User/UpdateProfile",
url: "/api/User/UpdateProfile/" + profile,
data: {sAppUser: profile},