Web API Put Request Generates an Http 405 Method Not Allowed Error

Web API Put Request generates an Http 405 Method Not Allowed error

So, I checked Windows Features to make sure I didn't have this thing called WebDAV installed, and it said I didn't. Anyways, I went ahead and placed the following in my web.config (both front end and WebAPI, just to be sure), and it works now. I placed this inside <system.webServer>.

<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/> <!-- add this -->
</modules>

Additionally, it is often required to add the following to web.config in the handlers. Thanks to Babak

<handlers>
<remove name="WebDAV" />
...
</handlers>

405 Method Not Allowed PUT

Did you decorate your action with HttpPut from the correct namespace?
Should look something like this :

[HttpPut]
public ActionResult ActionName()
{
//Your code
}

Edit: Apparently iis express has delete & put disabled by default.
If this only happens in iis express you might find this answer usefull.
Basicly you have to edit this file :

%userprofile%\documents\iisexpress\config\applicationhost.config 

On this line:

<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

And add the verb PUT

Edit nr 2:

Following This anwser : open up your iis manager and select you website.

Click handler mapping link on the right.

Find the ExtensionlessUrlHandler-Integrated-4.0 entry and double click it.

In Request Restrictions tab verbs you can then add put to enable put support.

HTTP/1.1 405 Method Not Allowed for WebApi(MVC 5) PUT Method

As I can see from your post, you're using IIS/8.0. The error 405: Method not allowed for the PUT, and also DELETE, verb is a well-known issue with this environment.

All evidence seems to centre around interference by an IIS module called WebDAV. You will need to either uninstall it globally or disable it locally for your project. In addition, you may need to edit your applicationhost.config file in order to allow the PUT verb.

If you know you won't need WebDAV, it's probably better to fully disable it or uninstall it from IIS. However, if you don't know if you or someone else sharing your server need it, then it's better to disable the features you don't need locally for your project in order to avoid causing problems for others.

I could either duplicate another SO Q&A here or you could just take a look at the accepted answer to this question and also the non-accepted answer to this question.

People normally frown upon answers that are based on information contained in links but, since they are both in SO, I'm hoping they will stay active as long as your question is active. If you find any of the information useful, please consider upvoting them.

Finally, this (external to SO) link also explains how to globally uninstall the WebDAV feature from the IIS Roles and Features configuration.

.netcore PUT method 405 Method Not Allowed

Using the [HttpPut("{id:int}")] route attribute, you will need to refer your api with: http://localhost:5000/api/masters/{id}

In your exmample:

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

So the id in the param also not needed:

[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.

405 Method Not Allowed for PUT and DELETE in ASP.NET WebAPI

If you are using Visual Studio 2012 or later to develop a Web API application, IIS Express is the default web server. This web server is a scaled-down version of the full IIS functionality that ships in a server product, and this web server contains a few changes that were added for development scenarios. For example, the WebDAV module is often installed on a production web server that is running the full version of IIS, although it may not be in actual use.

The development version of IIS, (IIS Express), installs the WebDAV module, but the entries for the WebDAV module are intentionally commented out, so the WebDAV module is never loaded on IIS Express. As a result, your web application may work correctly on your local computer, but you may encounter HTTP 405 errors when you publish your Web API application to your production web server.

Since your DEV server has full IIS functionality (with WebDAV) it will register multiple handlers for the same verb/method and one of the handlers is blocking the expected handler from processing the request.

So, the WebDAV is overriding your HTTP PUT and DELETE. During the processing of an HTTP PUT request, IIS calls the WebDAV module, When the WebDAV module is called, it checks its configuration and sees it is disabled, so it will return HTTP 405 Method Not Allowed error for any request that resembles a WebDAV request.

You can read more here https://learn.microsoft.com/en-us/aspnet/web-api/overview/testing-and-debugging/troubleshooting-http-405-errors-after-publishing-web-api-applications

To disable WebDAV add the following into your web.config

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

http error 405 method not allowed error with web.API

Okay, posted too quickly. I opened Windows program and features and then expanded IIS and unchecked webDAV. It removed it from IIS and then everything worked.

If anyone else finds this, please note that commenting out the lines in IIS did not work, I had to unistall the webDAV from IIS.

Sample Image

Error 405 not allowed on HttpPost in Asp.net core 3.1 web api

Firstly,as maghazade said,there are two endpoints for the post method.You can try to use [HttpPost("teststring")] as ferhrosa said.You can also add [action] to [Route("api/[controller]")],so that routes of actions will include their action names.If you add other actions with post method,the routes will not conflict anymore.

[Route("api/[controller]/[action]")]
[ApiController]
public class VideoWallController : ControllerBase
{
// GET: api/<ValuesController>/Get
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}

// GET api/<ValuesController>/Get/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}

// POST api/<ValuesController>/prova
[HttpPost]
public string prova()
{
return "true;";
}
// POST api/<ValuesController>/teststring
[HttpPost]
public void teststring([FromBody]string test)
{

}

// PUT api/<ValuesController>/Put/5
[HttpPut("{id}")]
public void Put(int id, [FromBody] string value)
{
}

// DELETE api/<ValuesController>/Delete/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}

And if you want to pass string test from Body,you need to add [FromBody] and select JSON in postman.
Sample Image

result:
Sample Image

405 method not allowed Web API

You are POSTing from the client:

await client.PostAsJsonAsync("api/products", product);

not PUTing.

Your Web API method accepts only PUT requests.

So:

await client.PutAsJsonAsync("api/products", product);


Related Topics



Leave a reply



Submit