【发布时间】:2018-11-12 20:53:35
【问题描述】:
即使我在启动类中添加了 useCors,仍然出现 Access-Control-Allow-Origin 错误。我也在使用 angular 5。我曾经在运行项目时遇到此错误,但我通过在启动时添加 cors 来修复它,但现在当我尝试从我的 angular 项目调用 post api 方法时得到它。
我的 startup.cs 代码:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
【问题讨论】:
-
用 MVC 检查一下。 docs.microsoft.com/en-us/aspnet/core/security/…
-
我使用 API .net core
-
那你为什么要在其中添加 MVC @Aleksandar ?
-
你能通过邮递员达到你的 api 行动吗?如果你不能问题来自你的 mvc 路由,你应该在你的问题中粘贴更多代码,顺便说一下我在回答中发布了一个技巧(代理)。
标签: c# .net angular api asp.net-core-webapi