.NET Core - Method not allowed on PUT and DELETE requests

by Matt Fantinel
14 Mar 2019 - 2 min read

Recently, I've been working on an Angular app, powered with a .NET Core API. Everything was working well and smoothly when running both on my local machine.

After pushing code to the beta server, most of it was working fine. The front-end was being able to call GET, POST and OPTIONS requests normally. When trying to DELETE or PUT entries, however, the following error would show up on the browser's console:

405 (Method Not Allowed) | Access to XMLHttpRequest at '...' from origin '...' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Method not allowed? CORS error? What?

Initially, I was misled by the CORS error. Having had problems with this in the past, I thoroughly checked my API code for any possible problem in the configuration that could lead to this error only after deployed. I found none.

Then, it dawned on me that the CORS error could be not because my API wasn't sending the 'Access-Control-Allow-Origin' on its response, but because it wasn't being sent on the HTTP 405 error seen above.

Solution

I ended up finding this article. What happens is that, when published, .NET Core enables the WebDAVModule, which disables PUT and DELETE requests by default.

So, to solve the issue, I ended up disabling WebDAV in the whole application, by adding these lines to the auto-generated web.config:

web.config
xml
<system.webServer>
  <modules runAllManagedModulesForAllRequests="false">
    <remove name="WebDAVModule" />
  </modules>
</system.webServer>

After restarting the API in IIS, TA-DA! Everything (or at least your PUT and DELETE requests) should be working normally.

This issue seems to only occur when hosting .NET Core on Windows, which is why I could not simulate the issue by running my application in production mode on my Linux machine.

I hope this will be helpful to someone like it was for me!

Written by

Matt Fantinel

I’m a web developer trying to figure out this weird thing called the internet. I write about development, the web, games, music, and whatever else I feel like writing about!

About

Newsletter? I have it!

I have a newsletter as another way to share what I write on this blog. The main goal is to be able to reach people who don't use RSS or just prefer getting their articles delivered straight into their inbox.

  • No fixed frequency, but at least once at month;
  • I do not plan on having content exclusive to the newsletter. Everything on it will also be on the blog;
  • I will never, ever ever ever, send you any form of spam.

You might also like

View blog

MongoDB on Linux - Data directory /data/db not found

1 min read

Fix the error that occurs when trying to run freshly-installed MongoDB on a Linux machine.

Back-End
Read

.NET Core - Project version mismatch

1 min read

Solving this cryptic error might take you many hours. Hopefully this will help you out.

Back-End
Read

Setting up Storybook on an Astro project

7 min read

I really, really thought this was gonna be easy.

Front-End
Read

Separating my website's content from its code

4 min read

Having an open source website is great, but having the content stored in the same spot as the code has some issues. On this post I walk through what I did to keep them separated.

Meta
Read