asp.net-mvc 405 消息,Web Api 不允许方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12807331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:20:18  来源:igfitidea点击:

405 Message, method not allowed with Web Api

asp.net-mvcasp.net-web-api

提问by user1166905

I have got the following on an API controller:

我在 API 控制器上有以下内容:

public void UpdateClient(Client client)
    {
        try
        {
            if (ModelState.IsValid)
            {
                db.Entry(client).State = EntityState.Modified;
                db.SaveChanges();
            }
        }
        catch
        {
            throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
        }
    }

And the following on the page:

以及页面上的以下内容:

$.ajax({
            url: "api/client/UpdateClient",
            type: "PUT",
            contentType: 'json',
            data: ko.toJSON(model.selectedClient()),
            success: function (result) {
                getClients();
                $("#loader").hide();
            },
            failure: function (result) {
                alert(result.d);
                $("#loader").hide();
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("An error occurred, please try again.");
                $("#loader").hide();
            }
        });

But this gives the error 405 Method Not Allowed, can anyone see where I may have gone wrong? For reference the url for the api is ok as I use the same api controller for other functions too.

但这给出了错误 405 Method Not Allowed,谁能看到我哪里出错了?作为参考,api 的 url 没问题,因为我对其他功能也使用相同的 api 控制器。

Also the selectedClient() is a Client object received via WebApi so should match perfectly to PUT up again.

此外 selectedClient() 是通过 WebApi 接收的 Client 对象,因此应该与 PUT 再次完美匹配。

采纳答案by user1166905

Looks like these two lines were wrong, I changed them as follows:

貌似这两行写错了,我改成如下:

contentType: 'application/json',
data: "{client: " + ko.toJSON(model.selectedClient()) + "}",

And now goes in.

现在进去了。

回答by Salman

If you are using IIS7 and have WebDav installed, try removing it. I was getting the same error only with the PUT verb and it solved the issue

如果您使用的是 IIS7 并安装了 WebDav,请尝试将其删除。我只在 PUT 动词上遇到了同样的错误,它解决了这个问题

Update: You can read about WebDav here: http://www.iis.net/learn/get-started/whats-new-in-iis-7/what39s-new-for-webdav-and-iis-7

更新:您可以在此处阅读有关 WebDav 的信息:http://www.iis.net/learn/get-started/whats-new-in-iis-7/what39s-new-for-webdav-and-iis-7

回答by Maggie Ying

Do you have [HttpPut] attribute on your UpdateClient action? Also, do you have a route that takes in the {action} as the routeTemplate? For example:

您的 UpdateClient 操作是否具有 [HttpPut] 属性?另外,您是否有将 {action} 作为 routeTemplate 的路线?例如:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

One more thing, try 'application/json' for the content-type in your ajax code instead of 'json'.

还有一件事,尝试在 ajax 代码中为内容类型使用“application/json”而不是“json”。

回答by JSideris

Note to future troubleshooters: I got this error when my "Put" controller was unintentionally expecting an additional parameter that wasn't being used.

对未来故障排除者的注意:当我的“Put”控制器无意中期望一个未被使用的附加参数时,我收到了这个错误。

回答by Austin Salgat

My issue was that under project properties, I was using Local IIS instead of IIS Express, and it defaulted to port 80 which blocked DELETE requests. Changing to IIS Express fixed it.

我的问题是在项目属性下,我使用的是本地 IIS 而不是 IIS Express,它默认为端口 80,它阻止了 DELETE 请求。更改为 IIS Express 修复了它。