2014-07-22 3 views
2

System.Web.Optimization을 다시 컴파일하여 Bundle.cs의 캐시 헤더를 변경하려는 경우 (CDN이 Vary 헤더를 좋아하지 않음) 다른 방법이 없어 보이기 때문에 헤더를 무시하십시오. Resharper를 통해 소스를 디 컴파일하여 변경하고 소스를 다시 컴파일 할 수는 있지만 프로젝트에 대한 참조를 추가하면 모든 종속 Nuget 패키지에서 오류가 발생합니다. 아래의 것과 유사합니다.System.Web.Optimization을 다시 컴파일하여 캐시 헤더 변경

'System.Web.Optimization.IBundleBuilder'형식이 참조되지 않은 어셈블리에 정의되어 있습니다. 어셈블리 'System.Web.Optimization, Version = 1.1.0.0, Culture = neutral, PublicKeyToken = 31bf3856ad364e35'에 대한 참조를 추가해야합니다.

모든 종속성을 컴파일하지 않아도됩니다. 나는 캐시 헤더를 오버라이드 (override)하는 다른 방법에 대해서도 개방되어있다. HTTPModules, IIS 등

답변

2

번들 사용자 지정 버전을 다시 컴파일하는 대신 번들 요청을 다른 HttpHandler를 통해 라우팅하기로 결정했습니다. URL을 빠르게 대체하면 번들의 내용을 쉽게 얻고 원하는 캐시 헤더로 작성할 수 있습니다. 가장 바람직한 방법은 아니지만 작동합니다.

라이브러리에서 고유 한 헤더를 설정할 수 없도록하는 것은 큰 시야입니다. 나는 그들이 이것을 곧 고치기를 바란다.

public void ProcessRequest(HttpContext context) 
    { 
     var request = context.Request; 
     var response = context.Response; 
     var cache = response.Cache; 

     var path = request.Url.LocalPath; 
     var bundlesPath = "~/" + path.Substring(path.IndexOf("mypath")); 
     bundlesPath = bundlesPath.Replace("mypath", "bundle"); 


     Bundle bundle = BundleTable.Bundles.GetBundleFor(bundlesPath); 
     var bundleContext = new BundleContext(new HttpContextWrapper(HttpContext.Current), BundleTable.Bundles, bundlesPath); 
     var bundleResponse = bundle.GenerateBundleResponse(bundleContext); 

     cache.SetCacheability(HttpCacheability.Public); 
     cache.SetExpires(DateTime.UtcNow.AddYears(1)); 
     cache.SetMaxAge(new TimeSpan(365, 0, 0, 0)); 
     cache.SetValidUntilExpires(true); 

     // This handler is called whenever a file ending 
     // in .sample is requested. A file with that extension 
     // does not need to exist. 
     response.ContentType = bundleResponse.ContentType; 
     response.Write(bundleResponse.Content); 
    }