简体   繁体   English

.netcore PUT 方法 405 Method Not Allowed

[英].netcore PUT method 405 Method Not Allowed

I have a simple model as that has 2 fields and with the following put method, i'm tring to update it in the db.我有一个简单的模型,因为它有 2 个字段,并且使用以下 put 方法,我想在数据库中更新它。 Everymethod including delete works, however put method always returns a 405 error in Postman.包括删除在内的所有方法都有效,但是在 Postman 中 put 方法总是返回 405 错误。 (tried WebDAV solutions as well.) What am I missing here? (也尝试过 WebDAV 解决方案。)我在这里错过了什么?

在此处输入图片说明

PUT METHOD:放置方法:

{
    "MasterId":1,
    "MasterName":"Test"
}

Action行动

[HttpPut("{id:int}")]
public async Task<IActionResult> PutMaster(int id, Master master)
{
    if (id != master.MasterId)
    {
        return BadRequest();
    }

    //...some code
    return NoContent();
}

If you are using IIS to run your application and have WebDav module, that can be an issue.如果您使用 IIS 来运行您的应用程序并拥有 WebDav 模块,这可能是一个问题。 For some strange reason WebDav does not allow PUT.由于某些奇怪的原因,WebDav 不允许 PUT。

I just uninstalled it, and it helped.我刚刚卸载了它,它有帮助。

Using the [HttpPut("{id:int}")] route attribute, you will need to refer your api with: http://localhost:5000/api/masters/ {id}使用[HttpPut("{id:int}")]路由属性,您需要将您的 api 引用为: http://localhost:5000/api/masters/ {id}

In your exmample:在您的示例中:

PUT http://localhost:5000/api/masters/1 PUT http://localhost:5000/api/masters/1

So the id in the param also not needed:所以参数中的id也不需要:

[HttpPut("{id:int}")] 
public async Task<IActionResult> PutMaster(Master master)

And it is a bad practice to expose entity framework object to the client, you should use a DTO class and map it to the entity framework object.将实体框架对象暴露给客户端是一种不好的做法,您应该使用 DTO 类并将其映射到实体框架对象。

First observation is that the called URL第一个观察是被调用的 URL

api/masters

does not match the route template of the controller action [HttpPut("{id:int}")] which would map to a URL like与控制器操作[HttpPut("{id:int}")]的路由模板不匹配,该模板将映射到类似的 URL

api/masters/{id:int}

Call the correct URL that matches the action's route template调用与操作的路由模板匹配的正确 URL

PUT api/masters/1

The error itself is because most likely you have another route that matches the provided URL but not the HTTP Verb.错误本身很可能是因为您有另一个与提供的 URL 匹配但与 HTTP 动词不匹配的路由。 like a root [HttpGet] on one of the controller actions.就像控制器操作之一上的根[HttpGet] This explains why you get a 405 Method Not Allowed error and not a 404 Not Found error这解释了为什么您会收到405 Method Not Allowed错误而不是404 Not Found错误

Your method should be你的方法应该是

 [HttpPut("{id:int}")]
        public async Task<IActionResult> PutMaster(int id,[FromBody]  Master master)
{}

You need to add [FromBody] attribute in the method.您需要在方法中添加[FromBody]属性。

Request url should be请求网址应该是

PUT api/masters/1

If your application is being hosted under IIS, running API in ASP.NET Core, include this line in the Web.Config file如果您的应用程序托管在 IIS 下,在 ASP.NET Core 中运行 API,请在 Web.Config 文件中包含此行

<configuration> 
    <system.webServer>
        <modules>
             <remove name="WebDAVModule" />
        </modules>
    </system.webServer>
</configuration>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM