2010-06-14 102 views
0

我在使AppendHeader正常工作時遇到問題,如果我也在使用授權過濾器。我爲我的AJAX操作使用了ActionFilter,這些操作應用了Expires,Last-Modified,Cache-Control和Pragma(雖然在測試時我已經嘗試在操作方法本身中包含它,而且結果沒有變化)。ASP.net AppendHeader在ASP MVC中不工作

如果我沒有授權過濾器,那麼標頭工作正常。一旦我添加過濾器,我嘗試添加的標題就會被刪除。

的頭我想補充

 
Response.AppendHeader("Expires", "Sun, 19 Nov 1978 05:00:00 GMT"); 
Response.AppendHeader("Last-Modified", String.Format("{0:r}", DateTime.Now)); 
Response.AppendHeader("Cache-Control", "no-store, no-cache, must-revalidate"); 
Response.AppendHeader("Cache-Control", "post-check=0, pre-check=0"); 
Response.AppendHeader("Pragma", "no-cache"); 

從正確的頁面標題的例子:

Server ASP.NET Development Server/9.0.0.0 
Date Mon, 14 Jun 2010 17:22:24 GMT 
X-AspNet-Version 2.0.50727 
X-AspNetMvc-Version 2.0 
Pragma no-cache 
Expires Sun, 19 Nov 1978 05:00:00 GMT 
Last-Modified Mon, 14 Jun 2010 18:22:24 GMT 
Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Content-Type text/html; charset=utf-8 
Content-Length 352 
Connection Close 

而且從一個不正確的頁面:

 
Server ASP.NET Development Server/9.0.0.0 
Date Mon, 14 Jun 2010 17:27:34 GMT 
X-AspNet-Version 2.0.50727 
X-AspNetMvc-Version 2.0 
Pragma no-cache, no-cache 
Cache-Control private, s-maxage=0 
Content-Type text/html; charset=utf-8 
Content-Length 4937 
Connection Close 

回答

0

要管理輸出緩存你可以使用OutputCache屬性對你的動作

編輯

如果你正在尋找的AuthorizeAttribute源代碼,你會看到它覆蓋輸出緩存策略,原因是在此代碼的註釋:

if (AuthorizeCore(filterContext.HttpContext)) { 
     // ** IMPORTANT ** 
     // Since we're performing authorization at the action level, the authorization code runs 
     // after the output caching module. In the worst case this could allow an authorized user 
     // to cause the page to be cached, then an unauthorized user would later be served the 
     // cached page. We work around this by telling proxies not to cache the sensitive page, 
     // then we hook our custom authorization code into the caching mechanism so that we have 
     // the final say on whether a page should be served from the cache. 

     HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache; 
     cachePolicy.SetProxyMaxAge(new TimeSpan(0)); 
     cachePolicy.AddValidationCallback(CacheValidateHandler, null /* data */); 

... 
} 
+0

它的工作,但爲什麼用一個授權屬性覆蓋我使用的標題,當它工作正常沒有它? – Chao 2010-06-15 08:49:05

+0

@潮看到我的編輯 – Gregoire 2010-06-15 13:30:09