[英]Access to XMLHttpRequest at 'http://localhost:25776/api/Upload' from origin 'http://localhost:4200' has been blocked by CORS policy
I am running angular spa application at http://localhost:4200
and .net core 3 application at http://localhost:25667
.我在
http://localhost:4200
运行 angular spa 应用程序,在http://localhost:25667
运行 .net core 3 应用程序。
When my angular SPA keeps getting following error message.当我的 Angular SPA 不断收到以下错误消息时。
Access to XMLHttpRequest at 'http://localhost:25776/api/Upload' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Even though I think my CORS are configured correctly.即使我认为我的 CORS 配置正确。
Here is my startup.cs
file.这是我的
startup.cs
文件。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy(
name: "CorsPolicy",
builder => builder.WithOrigins("http://localhost:4200/")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
});
services.AddHttpContextAccessor();
services.AddControllers().AddNewtonsoftJson();
}
// 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.UseCors("CorsPolicy");
app.UseAuthorization();
app.UseDefaultFiles();
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Upload")),
RequestPath = new PathString("/Upload")
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapFallbackToController("Index", "Fallback");
});
}
and here is my controller file这是我的控制器文件
using System.Collections.Generic;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using TestApp.API.Models;
using TestApp.API.Services;
namespace TestApp.API.Controllers
{
[ApiController]
[Route("api/[controller]")]
[EnableCors("CorsPolicy")]
public class UploadController : ControllerBase
{
// GET api/value
[HttpGet]
public IEnumerable<Upload> GetTempUploadData()
{
UploadServices uploadServices = new UploadServices();
return uploadServices.GetTempUploadData();
}
}
}
I also have Microsoft.AspNetCore.Cors nuget package installed.我还安装了Microsoft.AspNetCore.Cors nuget 包。
Any help would be highly appreciated.任何帮助将不胜感激。
Thank you in advance.先感谢您。
You need to remvoe this /
in WithOrigins
.您需要remvoe本
/
中WithOrigins
。
services.AddCors(options =>
{
options.AddPolicy(
name: "CorsPolicy",
builder => builder.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.SetIsOriginAllowed((host) => true));
});
I am posting my solution, in case someone run into similar issue.我正在发布我的解决方案,以防有人遇到类似问题。
Issue was with launchSetting.json
file within my API.问题在于我的 API 中的
launchSetting.json
文件。 I set anonymousAuthentication
to true
and it solved the issue.我将
anonymousAuthentication
设置为true
并解决了这个问题。 It had nothing to do with CORS setting.它与 CORS 设置无关。
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:25776/",
"sslPort": 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.