2014-01-17 35 views
1

MVCOutputCacheAttribute能夠防止它被裝飾(如果相對緩存中存在)屬性如何防止執行MVC操作?

如何實現一個自定義屬性相同的機制操作的執行?

在其他作品中,我希望能夠修飾一個動作,並根據屬性內部的邏輯決定動作是否應該執行。

附加

我實現由其中,如果給動作的請求與像flushaction=flush_silent查詢字符串到達​​時,自定義屬性(其延伸的OutputCacheAttribute)無效高速緩存的機制。

我也想這樣做,是不是要執行的操作:

[JHOutputCache(CacheProfile = "ContentPageController.Index")] 
public ActionResult Index(string url) 
{ 
    //custom code that should not execute when flushing the cache 
} 

回答

2

由於JHOutputCache延伸OutputCacheAttribute,從ActionFilterAttribute派生,阻止潛在的行動的執行相當簡單:

public class JHOutputCacheAttribute : OutputCacheAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     if (condition) 
      filterContext.Result = new EmptyResult(); 
     else 
      base.OnActionExecuting(filterContext); 
    } 
} 

您可以在此返回任何有效的ActionResult,包括您可能導出的任何自定義ActionResult

+1

正是我在找的東西! :) –

+0

我遇到了另一個問題,問題是使用'flushaction = flush_silent'的實際請求被緩存,並且不會再執行第二次。你知道如何防止它從屬性本身中緩存嗎?如果更容易,我可以設置其他問題。 –

+0

@GiuseppeR有點難以分辨你想做什麼,但聽起來好像[VaryByParam](http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.85% 29.aspx)可能是你需要看的。 –

相關問題