2011-11-11 4 views
2

내 웹 사이트에 만료 헤더를 추가하여 파일의 캐시 시간을 설정할 수 있습니다.Web.Config 파일에 만료 헤더 추가

나는 다음 예제를 찾았지만 가능하면 JPG, PNG, GIF, JS 및 CSS 파일 만 캐시하도록 설정하고 싶습니다.

<system.webServer> 
    <staticContent> 
     <clientCache cacheControlMaxAge="14.00:00:00" cacheControlMode="UseMaxAge"/> 
    </staticContent> 
</system.webServer> 

어떤 도움을 주셔서 감사합니다!

답변

0

특정 항목을 캐시하려면이 작업을 프로그래밍 방식으로 수행해야합니다. 다음 코드를 사용하면 갈 수 있습니다. Microsoft에서 제공 한 것입니다. 이제 막 가져와서 찾지 않아도됩니다. http://msdn.microsoft.com/en-us/library/ff477235.aspx

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Runtime.Caching; 
using System.IO; 

public partial class _Default : System.Web.UI.Page 
{ 


    protected void Button1_Click1(object sender, EventArgs e) 
    { 
     ObjectCache cache = MemoryCache.Default; 
     string fileContents = cache["filecontents"] as string; 

     if (fileContents == null) 
     { 
      CacheItemPolicy policy = new CacheItemPolicy(); 
      policy.AbsoluteExpiration = 
       DateTimeOffset.Now.AddSeconds(10.0); 

      List<string> filePaths = new List<string>(); 
      string cachedFilePath = Server.MapPath("~") + 
       "\\cacheText.txt"; 

      filePaths.Add(cachedFilePath); 

      policy.ChangeMonitors.Add(new 
       HostFileChangeMonitor(filePaths)); 

      // Fetch the file contents. 
      fileContents = File.ReadAllText(cachedFilePath) + "\n" 
       + DateTime.Now.ToString(); 

      cache.Set("filecontents", fileContents, policy); 

     } 

     Label1.Text = fileContents; 
    } 
} 
4

당신이 할 수있는 일은 파일이있는 폴더에 web.config 파일을 만드는 것입니다. (이미지의 경우 "이미지", 자바 스크립트 파일의 경우 "Js", 스타일 시트의 경우 "Css"등의 폴더가있을 수 있습니다.) 그런 다음 yor 코드를 붙여 넣습니다. 이렇게하면 파일 형식에 관계없이 해당 폴더에있는 모든 파일에 캐시 설정을 적용 할 수 있습니다. 이는 특정 파일 확장명에 캐시 설정을 적용하는 것보다 유연한 방법입니다.

3

IIS는 정적 콘텐츠에 대해 동적 만료 헤더를 지원하지 않습니다. 당신은 정적을 추가 할 수 있습니다

헤더이 방법을 만료 :

<system.webServer> 
    <staticContent> 
     <clientCache httpExpires="Sun, 29 Mar 2020 00:00:00 GMT" cacheControlMode="UseExpires" /> 
    </staticContent> 
</system.webServer> 

출처 : IIS 7.5 How do you add a Dynamic HTTP Expires Header

1

가 이미 다른 web.config에서를 사용하여 지적 : The Official Microsoft IIS site

여기에 비슷한 질문이 있습니다 특정 폴더가 아마도 최상의 옵션입니다.

당신은과 캐시 제어 헤더 및 아웃 바운드 재 작성 규칙 재정의 할 수 있습니다 그러나 :

<system.webServer> 
... 
<rewrite> 
    <outboundRules> 
    <rule name="RewriteCacheControlForHTMLFiles" preCondition="jsFile"> 
     <match serverVariable="RESPONSE_Cache_Control" pattern=".*" /> 
     <action type="Rewrite" value="max-age=86400" /> 
    </rule> 
    <preConditions> 
     <preCondition name="jsFile"> 
      <add input="{REQUEST_FILENAME}" pattern="\.js$" /> 
     </preCondition> 
    </preConditions> 
    </outboundRules> 
    ...