【发布时间】:2019-09-08 05:09:19
【问题描述】:
我有一个 asp.net 核心 API,我试图从它向另一个 asp.net 核心 API 发出 DELETE 请求。当我调用第一个 API 时,我在对第二个 API 进行删除调用时收到 405 错误。我已经尝试过这里找到的解决方案; ASP.NET Core with IIS - HTTP Verb Not Allowed 和其他几个相关的解决方案都没有成功。我还在我的 Startup.cs 文件中尝试了Enabling Cross-Origin Requests (CORS),但似乎没有任何改变。
这是我的删除端点:
[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
}
这是我的 web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
</configuration>
这是我在 Startup.cs 中的 ConfigureServices 和 Configure 方法:
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();
}
关于我可能做错的任何想法?
【问题讨论】:
-
详细的错误信息是什么?你在第二个api项目中配置
services.AddCors了吗?您需要在要向其发送请求的 api 中配置 cors。
标签: c# iis asp.net-core http-status-code-405