My Entity Framework Core API works fine locally, but fails with a 405 error in production

Viewed 433

I have a .NetCore 3.1 API project that uses Entity Framework.

It is working perfectly when I run it locally from Visual Studio.

However, after publishing to a production IIS 8.5 server, I get the following errors when I try to hit an API that uses PUT on the production server.

xhr.js:178 PUT https://nationalparks.xyz.gov/api/plants/91 405 (Method Not Allowed)

My controller begins like this:

    [HttpPut("{id}")]
    public async Task<IActionResult> PutPlant(long id, [FromBody] Plant plant)
    {
      ...
    }

And client-side looks like this:

    await axios({
        method: "PUT",
        url: "api/plants/" + plant.id,
        data: JSON.stringify(plant),
        headers: { 'Content-Type': 'application/json; charset=utf-8' }
    });

Honestly I'm stumped...I'm not sure why it's doing that.

I've seen a few posts saying to modify the web.config, but my app uses appsettings.json, not a web.config.

Any ideas?

Thanks!

2 Answers

The reason behind the issue is the WebDAV module intercepted the request. to resolve the issue you could follow the below workaround.

1)Remove WebDAV from your system:

open control panel-> “Turn Windows Features On or Off”->un-ticking the WebDAV publishing checkbox.

enter image description here

or

2)Add below code in your web.config file:

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

Please remember that after doing these changes you can nit use the WebDAV module in iis.

dotnet core applications do use web.config when running under IIS. You will notice that the publish command makes a web.config file that tells IIS which DLL to run for your application.

I think I had the same issue a while back. I fixed it by adding lines to web.config to remove the WebDAV modules. See this answer for more detail:

https://stackoverflow.com/a/12443578/65432

Related