Search code examples
asp.net-core-webapiwebapihttp-status-code-405

ASP.NET Core Web API DELETE Call Returns 405


MY asp.net core web api is returning a 405 when I try a DELETE call. When the API is running in my local machine I can send the delete call successfully.

In IIS under the site's Handler Mappings WEbDAV has DELETE listed as one of the verbs to be handled: IIS WebDAV settings

web.config has DELETE listed as a specified verb as well

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="GET,POST,PATCH,DELETE" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\ServiceCatalog.API.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

The delete endpoint works when testing from my local computer but once deployed I get the 405 return message.

In case it helps here is the code for the DELETE endpoint in the API

[HttpDelete("{id}")]
        public ActionResult DeleteImage(int id)
        {
            Images img = new Images();
            img.Imageid = id;

            //Check that the recrod exists to be deleted. IF not return NotFound status
            var imgCheck = _context.Images.Any(i => i.Imageid == img.Imageid);
            if (!imgCheck)
            {
                return NotFound("No record found to delete.");
            }

            //delete selected image record
            _context.Remove(img);
            _context.SaveChanges();

            // Return the id of the deleted record   
            return Ok("Image Id " + img.Imageid + " has been successfully deleted.");         

        }

Code for the ConfigureServices of the API

// Cors Configuration
            services.AddCors(o =>
            {
                o.AddPolicy(CorsPolicy,
                    builder => builder
                    .WithOrigins("http://xxxweb011",
                                 "http://xxxweb011.x.com")
                    .AllowAnyHeader()
                    .AllowAnyMethod());
            });

            
            services.AddMvc(o =>
            {
                o.ReturnHttpNotAcceptable = true;
            });


            // Connection string from environmental variables
            var connectionString = Configuration["connectionString:101stg"];
            // Connection to SQL
            services.AddDbContext<ServiceCatalogContext>(option => option.UseSqlServer(connectionString));


            services.AddControllers()
                    .AddNewtonsoftJson();


            services.AddOData();

            services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

Code for Configure

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {

            /*
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler(appBuilder =>
                {
                    appBuilder.Run(async context =>
                    {
                        context.Response.StatusCode = 500;
                        await context.Response.WriteAsync("An unexpected fault happened. Try again later.");
                    });
                });
            
            app.UseDeveloperExceptionPage();
        }
            */
            app.UseDeveloperExceptionPage();    //Keeping always shown during development

            app.UseRouting();


            //added for screenshot uplaod
            app.UseStaticFiles();
            
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.EnableDependencyInjection();
                endpoints.Select()
                            .OrderBy()
                            .Filter()
                            .SkipToken()
                            .MaxTop(100)
                            .Expand()
                            .Count();
                endpoints.MapODataRoute("api", "api", GetEdmModel());
            });
 
            app.UseCors(CorsPolicy);
            app.UseStatusCodePages();
            
        }

Solution

  • After trying to disable/modify all the WebDAV settings in IIS that I could find the only solution for me ended up being to comment out the following line of code in the applicationHost.config file

    <add name="WebDAVModule" />
    

    To be safe I also commented out the following line as well

    <add name="WebDAV" path="*" verb="PROPFIND,PROPPATCH,MKCOL,PUT,COPY,DELETE,MOVE,LOCK,UNLOCK" modules="WebDAVModule" resourceType="Unspecified" requireAccess="None" />