ASP.NET 核心网站 API 删除调用 Returns 405

ASP.NET Core Web API DELETE Call Returns 405

我的 asp.net 核心网站 api 在我尝试 DELETE 调用时 return 收到 405。当 API 在我的本地机器上是 运行 时,我可以成功发送删除调用。

在站点的处理程序映射下的 IIS 中,WEbDAV 已将 DELETE 列为要处理的动词之一: IIS WebDAV settings

web.config 也将 DELETE 列为指定动词

<?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>

删除端点在从我的本地计算机进行测试时有效,但部署后我收到 405 return 消息。

如果有帮助,这里是 API

中 DELETE 端点的代码
[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.");         

        }

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());

配置代码

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();
            
        }

在尝试 disable/modify IIS 中的所有 WebDAV 设置后,我能找到的唯一解决方案最终是注释掉 applicationHost.config 文件中的以下代码行

<add name="WebDAVModule" />

为了安全起见,我也注释掉了下面这行

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