Asp.net core 由于405 Http状态代码,无法调用DELETE webapi方法
我已经用.NETCore3.0预览版编写了API,并使用Angular 8作为前端。 我在用户控制器上有一个名为DeleteUser的方法。方法如下:Asp.net core 由于405 Http状态代码,无法调用DELETE webapi方法,asp.net-core,asp.net-core-webapi,Asp.net Core,Asp.net Core Webapi,我已经用.NETCore3.0预览版编写了API,并使用Angular 8作为前端。 我在用户控制器上有一个名为DeleteUser的方法。方法如下: //DELETE users/d18da620-6e4d-4b1c-a1ec-36d89145f4ca [HttpDelete("{id}")] public async Task<IActionResult> DeleteUser(Guid id) { ... } 执行此调用后,我在Visual Studio中获得以下输出
//DELETE users/d18da620-6e4d-4b1c-a1ec-36d89145f4ca
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteUser(Guid id) {
...
}
执行此调用后,我在Visual Studio中获得以下输出:
Microsoft.AspNetCore.Cors.Infrastructure.CorsService:Information: CORS policy execution successful.
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint '405 HTTP Method Not Supported'
但是,我已经设置了自定义CORS策略。在ConfigureServices中,我有以下内容:
services.AddCors(options => {
options.AddPolicy("CorsPolicy",
builder => builder
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true)
.AllowAnyHeader());
});
在配置方法中,我使用以下策略:
app.UseCors("CorsPolicy");
所以我的问题是,如何成功地调用这个delete方法?我错过了什么
编辑:这是我调用api的deleteUser方法
public deleteUser(userId) {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type' : 'application/json'
}),
body: {
id: userId
}
};
return this.httpClient.delete(this.USERSENDPOINT, httpOptions);
}
更新:
尝试在angular中调用您的方法,如下所示:
this.usersApiService.deleteUser(userId).subscribe((data) => {
...
}
this.httpClient.deletethis.USERSENDPOINT+/+userId
希望有帮助你能提供usersApiService.deleteUser代码吗?@Nikita,哦,是的,我在那里粘贴了错误的代码:谢谢。将其添加到我的questionBy spec中,删除调用中不能有正文。URL必须定义要删除的资源。使用此配置,我得到以下错误:HTTP错误500.19-内部服务器错误无法读取配置节“httpProtocol”,因为它缺少节声明。它不在system.webServer元素内。但是,在web.config中添加了此项后,现在我无法对整个集合执行基本的事件获取示例:Get/users JS控制台中有一个错误,指出CORS标头访问控制允许源与localhost:4200不匹配删除请求中的主体在哪里?我相信问题出在Angular而不是.NET中。。。我现在才想到这一点。