405 error when making DELETE request from asp.net core application


Caleb Powell

I have an asp.net core API from which I am trying to make a DELETE request to another asp.net core API. When I call the first API, when it makes a delete call to the second API, I get a 405 error. I've tried the solutions found here; ASP.NET Core with IIS - HTTP Verbs Not Allowed , and a few other related solutions, without success. I also tried enabling Cross-Origin Requests (CORS) in the Startup.cs file , but nothing seems to change.

Here is my delete endpoint:

    [HttpDelete("MyDeleteEndpoint")]
    public async Task MyDeleteEndpoint([FromRoute] string id)
    {
        var http = new HttpClient();

        var response = await http.DeleteAsync("https://myserver:443/api/mycontroller/{id});

        //do more stuff
    }    

Here is my web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="false">
      <remove name="WebDAVModule" />
    </modules>
  </system.webServer>
</configuration>  

Here is my ConfigureServices and Configure method in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

  services.AddCors(options =>
  {
      options.AddPolicy("mypolicy",
      builder =>
      {
          builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod();
      });
  });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
         app.UseHsts();
    }

    app.UseCors("mypolicy");
    app.UseHttpsRedirection();
    app.UseMvc();
}

Any ideas on what I might be doing wrong?

Martin

I had this problem before. The only way I've found to fix that is to explicitly list the methods allowed in .NET web.config.

Here is an excerpt web.config. Note that you probably don't want all of these settings, but I've left them here for brevity:

...
<httpProtocol>
  <customHeaders>
    <remove name="X-Powered-By" />
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Credentials" value="true" />
    <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS" />
    <add name="Access-Control-Allow-Headers" value="authorization,content-type" />
  </customHeaders>
</httpProtocol>
...

No need to explicitly add the DELETErequest to the allowed method, the request is automatically denied even before it reaches my code.

Related


Error 405 when sending post request to Asp.net from Angular

Raphael I am trying to send a post request from an Angular frontend to an ASP.net backend. Get request works fine. When I send: this.http.post(“ http:// localhost:3000 / api / Cards ”,“ some data”,httpOptions).subscribe(res => this.display(res)); I run to ASP.

Error 405 when sending post request to Asp.net from Angular

Raphael I am trying to send a post request from an Angular frontend to an ASP.net backend. Get request works fine. When I send: this.http.post(“ http:// localhost:3000 / api / Cards ”,“ some data”,httpOptions).subscribe(res => this.display(res)); I run to ASP.

405 error making axios put/patch request from vue to laravel

lala-nbk-2020 Help me, I can't get the update method to work with put/patch. There's no problem when it is either react, so maybe it vue's a problem, or I'm missing something simple. let formData = new FormData(); formData.append("image", this.imag

405 error making axios put/patch request from vue to laravel

lala-nbk-2020 Help me, I can't get the update method to work with put/patch. There's no problem when it is either react, so maybe it vue's a problem, or I'm missing something simple. let formData = new FormData(); formData.append("image", this.imag

405 error making axios put/patch request from vue to laravel

lala-nbk-2020 Help me, I can't get the update method to work with put/patch. There's no problem when it is either react, so maybe it vue's a problem, or I'm missing something simple. let formData = new FormData(); formData.append("image", this.imag

405 error making axios put/patch request from vue to laravel

lala-nbk-2020 Help me, I can't get the update method to work with put/patch. There's no problem when it is either react, so maybe it vue's a problem, or I'm missing something simple. let formData = new FormData(); formData.append("image", this.imag

Convert request from GET to POST in ASP.NET CORE APPLICATION

Simone Spain I need to convert request from get to post in two cases: - in case of standard controller - in case of web api in asp.net core c# project. Here is the method declaration. Standard controller: public async Task<IActionResult> Details(string id) We

Convert request from GET to POST in ASP.NET CORE APPLICATION

Simone Spain I need to convert request from get to post in two cases: - in case of standard controller - in case of web api in asp.net core c# project. Here is the method declaration. Standard controller: public async Task<IActionResult> Details(string id) We