2010-08-05 62 views
0

我一直在閱讀'ASP.NET-MVC Tutorials'以瞭解如何爲ASP.NET MVC應用程序中的'masterpage'視圖生成數據。它提出了使用「基礎控制器」並在其構造函數中生成數據的模式。如何在設置的時間內將ASP.NET主頁數據存儲在應用程序緩存中

我的問題是,我希望將應用程序數據存儲在應用程序緩存而不是viewdata字典中。應用程序高速緩存不像以後那樣存在於控制器構造函數中,我如何在應用程序高速緩存中存儲masterpage視圖的數據?

+0

什麼是控制器中的應用程序緩存?首先我聽說過它.. – Ahmad 2010-08-05 08:54:07

+0

@Ahmad這是正常的ASP.NET Web緩存。請參閱http://stackoverflow.com/questions/3412868/how-to-store-asp-net-masterpage-data-in-application-cache-for-set-duration/3413495#3413495 – bzlm 2010-08-08 21:33:46

回答

0

想通了。那麼....一個有效的版本。當ControllerActionInvoker的'InvokeAction'方法觸發時,我需要將應用程序數據添加到緩存中。要做到這一點,我不得不創建一個新的ActionInvoker如下。

public class ContextActionInvoker : ControllerActionInvoker 
{ 
    public const string testMessageCacheAndViewDataKey = "TESTMESSAGE"; 
    private const int testListLifetimeInMinutes = 10; 

    public ContextActionInvoker(ControllerContext controllerContext) : base() { } 

    public override bool InvokeAction(ControllerContext context, string actionName) 
    { 
     // Cache a test list if not already done so 
     var list = context.HttpContext.Cache[testMessageCacheAndViewDataKey]; 
     if (list == null) 
     { 
      list = new SelectList(new[] { 
       new SelectListItem { Text = "Text 10", Value = "10" }, 
       new SelectListItem { Text = "Text 15", Value = "15", Selected = true }, 
       new SelectListItem { Text = "Text 25", Value = "25" }, 
       new SelectListItem { Text = "Text 50", Value = "50" }, 
       new SelectListItem { Text = "Text 100", Value = "100" }, 
       new SelectListItem { Text = "Text 1000", Value = "1000" } 
      }, "Value", "Text"); 
      context.HttpContext.Cache.Insert(testMessageCacheAndViewDataKey, list, null, DateTime.Now.AddMinutes(testListLifetimeInMinutes), TimeSpan.Zero); 
     } 
     context.Controller.ViewData[testMessageCacheAndViewDataKey] = list; 

     return base.InvokeAction(context, actionName); 
    } 
} 

一旦這樣做,我需要創建一個自定義的ControllerFactory這將確保正確ActionInvoker方法被調用。我這樣做...

public class ContextControllerFactory : DefaultControllerFactory 
{ 
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
    { 
     IController controller = base.GetControllerInstance(requestContext, controllerType); 
     Controller contextController = controller as Controller; 

     if (contextController != null) 
     { 
      var context = new ControllerContext(requestContext, contextController); 
      contextController.ActionInvoker = new ContextActionInvoker(context); 
     } 
     return controller; 
    } 
} 

然後我不得不告訴MVC應用程序使用哪個controllerfactory。我做這個改變的Global.asax.cs有點...

public class MvcApplication : System.Web.HttpApplication 
{ 
    ... 

    protected void Application_Start() 
    { 
     ... 
     ControllerBuilder.Current.SetControllerFactory(typeof(ContextControllerFactory)); 
     ... 
    } 

    ... 
} 

然後在母版我使用的下拉列表HTML輔助方法,通過做...

<%: Html.DropDownList(MVC_MasterPage_data_set_in_cache.Controllers.ContextActionInvoker.testMessageCacheAndViewDataKey) %> 
0

如果覆蓋Initialize方法,您可以訪問CacheInitialize將在對控制器中的任何操作請求執行任何操作之前執行。

protected override void Initialize(RequestContext requestContext) 
{ 
    base.Initialize(requestContext); 
    var list = requestContext.HttpContext.Cache[testMessageCacheAndViewDataKey]; 
    if (list == null) { ... } 
} 
相關問題