在MVC6应用程序中启用CORS,Startup看起来像:
我在ConfigureServices()
方法中添加了以下代码:
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().AllowCredentials());
});
......
在配置Configure()
app.UseCors("AllowSpecificOrigin");
.......
我的代码尝试从另一个URL下载角度模板,templtes得到下载(在fiddler中看到)。但我的浏览器控制台说
XMLHttpRequest无法加载http://hsa-is-utl32.[mydomain].com/LayoutService//platform/templates/layout/topbanner.html。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许原点“http://mydevbox.[mydomain].com”访问。
没有任何疑问?
答案 0 :(得分:2)
我有类似的问题。不是100%肯定你的设置是什么,但也许这可以帮助。假设我有两个域,http://myapp.domain/我的应用程序位于哪里,http://templates.domain/template.html就是模板所在的域。这就是我做的。
这为MVC 6应用程序(myapp.domain)设置了CORS。
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder.WithOrigins("http://myapp.domain").AllowAnyHeader().AllowAnyMethod().AllowCredentials());
});
services.AddMvc();
}
public void Configure(IApplicationBuilder app)
{
app.UseIISPlatformHandler();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseCors("AllowSpecificOrigin");
//make sure its before UseMvc()!!
app.UseMvc();
}
然后我在http://templates.domain的web.config
设置了我的 wwwroot
文件,以允许来自http://myapp.domain/的CORS。这包括静态文件,例如来自不同域的角度引用模板。
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://myapp.domain" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
从那时起,简单地说,出于某种原因,我们想要从不同的域获取您的路由模板,您可以这样做:
(function () {
"use strict";
var app = angular.module("myApp", ['ngRoute']);
// configure the routes
app.config(['$provide', '$sceDelegateProvider', '$routeProvider', '$locationProvider', '$httpProvider',
function ($provide, $sceDelegateProvider, $routeProvider, $locationProvider, $httpProvider) {
$sceDelegateProvider.resourceUrlWhitelist(['self', 'http://templates.domain/**']);
$httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = true;
$httpProvider.defaults.useXDomain = true;
//routes
$routeProvider.when('/', {
templateUrl: 'http://templates.domain/template.html',
controller: 'templatesController'
})
.otherwise({ redirectTo: '/' });
$locationProvider.html5Mode(true);
$httpProvider.defaults.headers.common = {
'Content-Type': 'application/json, charset=UTF-8'
};
}]);
app.controller('templatesController', ['$scope', function ($scope) {
$scope.message = 'message from controller';
}]);
})();
在您的HTML中,只需添加<div ng-view></div>
并添加angular-routes
。
现在,您应该在角度应用程序中调用模板时在响应标题中看到此内容。
Access-Control-Allow-Origin:http://myapp.domain/
我希望这会有所帮助。
答案 1 :(得分:1)
Nick的web.config片段为我工作。我有类似的情况,在localhost IIS Express上运行两个应用程序。它在Startup.cs中没有app.UseCors(“AllowSpecificOrigin”)。
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:10001" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
答案 2 :(得分:0)
我建议检查从MVC应用程序返回的标头。请记住,如果您设置了AllowCredentials,则无法使用Access-Control-Allow-Origin = *。
尝试从MVC app中删除最后一个配置(我的意思是这个)。
.AllowCredentials());
如果您想设置creadentials,我相信您必须设置访问此网站的应用的Access-Control-Allow-Origin =&#39;网址&#39;
您还应该检查Fiddler的回复,看看出了什么问题。