【发布时间】:2021-02-18 15:04:53
【问题描述】:
我知道这个问题被问了很多次,但在线提供的解决方案对我不起作用。 我尝试像这样从 Angular 发出发布请求
ApiResponse(formVal,url){
return this._Service.AddUser(this.url,formInput)
.toPromise().then(res=>{
return res;
})
}
但我收到此错误 从源“http://localhost:4200”访问“http://localhost:61382/api”的 XMLHttpRequest 已被 CORS 策略阻止:没有“访问控制-请求的资源上存在 Allow-Origin' 标头。 即使我在 Asp.Net 中有 corsPolicy,这仅发生在 post 请求中。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(opt =>
{
opt.AddPolicy("CorsPolicy",
c => c.AllowAnyOrigin()
.AllowAnyHeader()
.AllowCredentials()
.AllowAnyMethod());
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseCors("CorsPolicy");
}
app.UseAuthentication();
app.UseMvc(routes => routes.MapRoute(
"default", "api/{controller}/{action}/{id?}"
));
}
}
你能帮帮我吗?谢谢!
【问题讨论】:
-
你能显示你的控制器的标题和你的动作吗?
标签: asp.net-core cors