我很难解决CORs问题,所以我想知道是否可以在与ASP .NET Core后端相同的主机和端口上托管一个单页Vue.js应用程序?
当前后端在localhost:5000上,客户端在localhost:8080上。这在调试中效果很好,我使用kestrel F5后端,并使用Vue CLI服务前端。工作正常。但是...
使用以下方式发布自托管应用程序时:
dotnet publish -c release -r win10-x64
然后启动独立的.exe,它将后端放在端口5001上的https上,并在8080上再次使用Vue CLI运行客户端。现在什么都行不通,我收到CORS错误:
访问“ http://localhost:5000/api/v1/students”处的XMLHttpRequest 来自来源“ http://localhost:8080”的信息已被CORS政策阻止: 请求中没有'Access-Control-Allow-Origin'标头 资源。
访问“ https://localhost:5001/notify/negotiate”处的XMLHttpRequest 来自来源“ http://localhost:8080”的信息已被CORS政策阻止: 对预检请求的响应未通过访问控制检查: 响应中“ Access-Control-Allow-Origin”标头的值必须 当请求的凭据模式为时,不是通配符“ *” '包括'。发起的请求的凭据模式 XMLHttpRequest由withCredentials属性控制。
我的创业公司是:
public void ConfigureServices(IServiceCollection services) {
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSpaStaticFiles(configuration => {
configuration.RootPath = "ClientApp/dist";
});
services.AddCors();
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
else {
app.UseHsts();
app.UseHttpsRedirection();
}
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseAuthentication();
app.UseCors(builder => builder.WithOrigins("http://localhost:8080").AllowAnyMethod().AllowAnyHeader().AllowCredentials());
app.UseSignalR(routes => { routes.MapHub<NotifyHub>("/notify"); });
app.UseMvc();
}
我不确定如何部署此应用。它似乎分为两个部分,需要分别启动。理想情况下,我希望它可以是一个,但如果可以与CORS一起使用,甚至可以是两个。
1)如何在同一IP和节点上启动,避免使用CORS? 2)如果以上都不可行,如何解决CORS问题?
谢谢。