我已將應用程序配置爲使用CORS。所有工程完全在我的本地環境,但部署應用天青後,我們收到以下錯誤:使用CORS的應用程序在Azure中不起作用
No 'Access-Control-Allow-Origin' header is present on the requested resource.
當我在我的電腦上運行的應用程序並創建使用DHC Chrome擴展起源頭,它實際工作:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://mysite.s3-website-eu-west-1.amazonaws.com
Access-Control-Expose-Headers: Location,Finished
這是代碼/配置,我用:
的Global.asax.cs
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
config.EnableCors(new MyCorsPolicyAttribute());
config.MapHttpAttributeRoutes();
}
MyCorsPolicyAttribute.cs
public class MyCorsPolicyAttribute : Attribute, ICorsPolicyProvider
{
private CorsPolicy _policy;
public MyCorsPolicyAttribute()
{
_policy = new CorsPolicy
{
AllowAnyMethod = true,
AllowAnyHeader = true,
SupportsCredentials = true
};
_policy.ExposedHeaders.Add("Location");
_policy.ExposedHeaders.Add("Finished");
// Add allowed origins.
_policy.Origins.Add("http://mysite.s3-website-eu-west-1.amazonaws.com");
_policy.Origins.Add("http://localhost:81");//for testing purposes;
}
public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request)
{
return Task.FromResult(_policy);
}
public Task<CorsPolicy> GetCorsPolicyAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return Task.FromResult(_policy);
}
}
終於的web.config
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="WebDav" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
爲什麼上述工作不在Azure環境中?
將它部署到一個新的應用程序服務,它工作!謝謝! – Dadria