我在这方面机智。我已经研究过类似问题的其他答案,因此没有任何运气。
我相当确定我已经正确启用了CORS,以允许来自所有来源的传入请求(在这种情况下为POST请求),但是我看到以下错误:
无法加载http:// localhost:5000 / expenses:所请求的资源上没有“ Access-Control-Allow-Origin”标头。因此,不允许访问源' http:// localhost:4200 '。响应的HTTP状态码为500。
这是我在webAPI项目中启用CORS的方式:
Startup.cs中的相关方法
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
services.AddDbContext<ExpensesDbContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));
services.AddTransient<IBaseDa<Accounts>, AccountsDataAccess>();
services.AddTransient<IExpensesDa, ExpensesDa>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
env.EnvironmentName = "Development";
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder => …Run Code Online (Sandbox Code Playgroud) 我创建了一个带有单个控制器的 ASP.Net CORE Web API 项目,现在想从客户端 (React) Web 应用程序调用它。
但是,调用失败,并显示“请求的资源上不存在‘Access-Control-Allow-Origin’标头。”。
从 Fiddler 调用同一个端点时,预期的响应标头不存在。
感谢 ATerry,我有了更深入的了解:标头不存在,因为 React Web 应用程序和 .Net Core Web API 托管在同一个盒子上。React 填充与 (API) 框相同的请求 Origin: 标头,因此服务器(非常聪明)不会添加 Allow-... 响应标头。然而,React 应用程序拒绝响应,因为缺少这些标头。
我正在使用 .Net Core v2.1(在撰写本文时最新)。
我构建的代码基于
https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-2.1
我检查了这些
https://weblog.west-wind.com/posts/2016/Sep/26/ASPNET-Core-and-CORS-Gotchas
...但没有一个建议奏效。
有任何想法吗?
这就是我配置 .Net Core 应用程序的方式(代码从实际更改为尝试并允许任何操作):
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. …Run Code Online (Sandbox Code Playgroud) 我在客户端有基于 Angular(7.2.1) 的 UI 的 Asp.Net Core(3) WebApi 项目。使用 Postman 或仅使用 URL 时,我可以使用 GET 和 POST 而不会出现任何特定错误。通过 Angular(Chrome\FireFox\IE Browser) 尝试相同的操作时,我收到以下错误。
zone.js:3243 OPTIONS http://localhost:8888/api/PaymentDetails 405 (Method Not Allowed)
ccess to XMLHttpRequest at 'http://localhost:8888/api/PaymentDetails' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我会尽量分享尽可能多的代码来演示流程。
WebApi Startup.cs:
公共 IConfiguration 配置 { 获取;}
// This method gets called by the runtime. Use this method to add …Run Code Online (Sandbox Code Playgroud) 我在 dotnet core 中遇到了一些有关 CORS/HttpClient 的问题。首先,我的应用程序的结构如下:
Webapp -> Microservice-Gateway -> multiple Microservices
Run Code Online (Sandbox Code Playgroud)
如果我从我的 Web 应用程序进行 Ajax 调用
$.ajax({
url: "https://[actual gateway Url]/customer/" + customerId,
type: "GET",
timeout: 60000,
crossDomain: true,
dataType: "json",
success: data => {
//...
}
});
Run Code Online (Sandbox Code Playgroud)
到网关服务器时,我有时会收到以下错误消息:
请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问来源“ http://localhost:50595 ”。响应的 HTTP 状态代码为 500。
然而,如果我调试,我的网关在内部会抛出此错误:
System.Net.Http.HttpRequestException:发送请求时发生错误。---> System.Net.Http.WinHttpException:操作超时
我尝试通过在网关和每个微服务中添加以下代码来解决第一个错误:
public void ConfigureServices(IServiceCollection services) {
var corsBuilder = new CorsPolicyBuilder();
corsBuilder.AllowAnyHeader();
corsBuilder.AllowAnyMethod();
corsBuilder.AllowAnyOrigin();
corsBuilder.AllowCredentials();
services.AddCors(options => {
options.AddPolicy("SiteCorsPolicy", corsBuilder.Build());
});
//..
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { …Run Code Online (Sandbox Code Playgroud) asp.net ×3
asp.net-core ×3
c# ×3
.net ×2
cors ×2
.net-core ×1
ajax ×1
angular ×1
httpclient ×1
reactjs ×1