Angular CORS策略已阻止从源站访问XMLHttpRequest:No';访问控制允许原点';请求的资源上存在标头
我已经在MVC核心中创建了一个API项目。在我的控制器中,我添加了一些GET和POST方法的api,它们与Postman一起工作得非常好。但当我尝试从Angular应用程序中调用它们时,它们会给我CORS错误: CORS策略已阻止从源站访问XMLHttpRequest:请求的资源上不存在“访问控制允许源站”标头 我在谷歌上搜索解决方案,发现我需要添加CORS NuGet包。我做到了,但错误仍然存在 以下是我的Angular CORS策略已阻止从源站访问XMLHttpRequest:No';访问控制允许原点';请求的资源上存在标头,angular,api,asp.net-core,cors,asp.net-core-mvc,Angular,Api,Asp.net Core,Cors,Asp.net Core Mvc,我已经在MVC核心中创建了一个API项目。在我的控制器中,我添加了一些GET和POST方法的api,它们与Postman一起工作得非常好。但当我尝试从Angular应用程序中调用它们时,它们会给我CORS错误: CORS策略已阻止从源站访问XMLHttpRequest:请求的资源上不存在“访问控制允许源站”标头 我在谷歌上搜索解决方案,发现我需要添加CORS NuGet包。我做到了,但错误仍然存在 以下是我的Startup.cs文件代码: using System; using System.C
Startup.cs
文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using webapp1.Model;
namespace webapp1
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
});
services.AddDbContext<TodoContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseCors(options =>
options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
}
}
}
您需要在Web Api中启用CORS。全局启用CORS的更简单和首选方法是将以下内容添加到web.config中
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>
更新:
在ASP.NETCore中,我们没有web.config,而是有app.config文件。您仍然需要有web.config,您可以添加web配置项模板。您可以使用该选项,如更改最大文件上载限制等
web.config文件是在发布项目时生成的。这个问题与angular标记有什么关系?angular通常运行一个web包开发服务器,默认情况下运行在端口4200上,而您的服务器通常运行在另一个端口上,该端口只允许来自同一来源的请求,因此它运行的端口是相同的,以便生成http来自dev服务器的请求是一个跨源请求,将被服务器阻止。要解决这个问题,您需要允许来自服务器的跨源请求,具体取决于您使用的服务器。但在Express中,您只需添加以下内容:const cors=require('cors');安装cors软件包后使用(cors({origin:,credentials:true}))我从
Startup.cs
中删除了cors代码。只需在web.config中添加这些设置即可。
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
</system.webServer>