我一直在阅读'ASP.NET-MVC Tutorials'以了解如何为ASP.NET MVC应用程序中的'masterpage'视图生成数据。它提出了使用“基础控制器”并在其构造函数中生成数据的模式。如何在设置的时间内将ASP.NET主页数据存储在应用程序缓存中
我的问题是,我希望将应用程序数据存储在应用程序缓存而不是viewdata字典中。应用程序高速缓存不像以后那样存在于控制器构造函数中,我如何在应用程序高速缓存中存储masterpage视图的数据?
我一直在阅读'ASP.NET-MVC Tutorials'以了解如何为ASP.NET MVC应用程序中的'masterpage'视图生成数据。它提出了使用“基础控制器”并在其构造函数中生成数据的模式。如何在设置的时间内将ASP.NET主页数据存储在应用程序缓存中
我的问题是,我希望将应用程序数据存储在应用程序缓存而不是viewdata字典中。应用程序高速缓存不像以后那样存在于控制器构造函数中,我如何在应用程序高速缓存中存储masterpage视图的数据?
想通了。那么....一个有效的版本。当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) %>
如果覆盖Initialize
方法,您可以访问Cache
。 Initialize
将在对控制器中的任何操作请求执行任何操作之前执行。
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
var list = requestContext.HttpContext.Cache[testMessageCacheAndViewDataKey];
if (list == null) { ... }
}
什么是控制器中的应用程序缓存?首先我听说过它.. – Ahmad 2010-08-05 08:54:07
@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