我遇到了一個問題,即在我的Web API中啓用了CORS。 OPTIONS請求到服務器返回正確的CORS頭。但是,如果我更改API的web.config中的任何內容,則不會返回CORS標頭 Access-Control-Allow-Origin:「*」。在IIS中不返回CORS響應標頭(間歇性地) - Web API
要讓Web.API再次運行,我必須對web.config進行更改。重新編譯我網頁API,並用不同的版本號部署,然後API將功能和選項,以在返回我的CORS頭飛行前檢查
編輯:增加註冊和啓用CORS系統代碼的Global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
log4net.Config.XmlConfigurator.Configure();
}
在WebApiConfig.cs
:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.EnableCors();
config.Formatters.JsonFormatter.
SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.MapHttpAttributeRoutes();
config.Services.Add(typeof(IExceptionLogger), new GlobalExceptionLogger());
config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());......
而且這個......我離開API操作,當我離開辦公室......然後幾個小時後,API停止再次合作,而無需改變一切。我在這裏猜測,但它看起來像應用程序池回收時不會調用application_Start例程,或者在回收等後遺忘了CORS和路由的配置...
你可以顯示啓用CORS的代碼嗎? – Keith