【发布时间】:2021-01-10 01:03:18
【问题描述】:
当我尝试 DELETE 调用时,我的 asp.net 核心 web api 返回 405。当 API 在我的本地机器上运行时,我可以成功发送删除调用。
在站点的 Handler Mappings 下的 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 返回消息。
如果有帮助的话,这里是 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 的 ConfigureServices 代码
// 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();
}
【问题讨论】:
-
您如何拨打删除电话?邮递员或一些前端客户端?
-
我使用 Postman 和我的 Web 前端进行了测试。当我在运行 API 删除调用时将它们指向我的本地机器时。当我尝试针对已发布的 API 时,我得到 405。找不到服务器上停止删除动词的内容。
-
所以其他动词在起作用?可以查看 iis 日志吗?
-
是的,GET、POST 和 PATCH 动词都可以正常工作。我去看看 IIS 日志。
-
如果有助于故障排除,我还从 API 添加了我的代码。
标签: asp.net-core-webapi webapi http-status-code-405