Asp.net mvc 如何知道给定控件的OutputCache是否正在使用?
我正在尝试在Asp.NETMVC中编写一个通用的“menu.ascx”用户控件,它将为我的应用程序生成一个格式正确的HTML菜单。菜单是根据数据库中的内容和一系列资源解析生成的。。。它们通过ViewModel上的属性传递到PartialView 利用menu.ascx控件上的OutputCache指令来限制到数据库和资源文件的往返次数是有意义的。我的意图是用VaryByParam=none和VaryByCustom属性标记OutputCache指令,在global.asax中实现自定义安全查找 我的问题是:我们如何知道何时将使用menu.ascx的OutputCache,以便在控制器中构建ViewModel时跳过数据获取操作Asp.net mvc 如何知道给定控件的OutputCache是否正在使用?,asp.net-mvc,user-controls,outputcache,Asp.net Mvc,User Controls,Outputcache,我正在尝试在Asp.NETMVC中编写一个通用的“menu.ascx”用户控件,它将为我的应用程序生成一个格式正确的HTML菜单。菜单是根据数据库中的内容和一系列资源解析生成的。。。它们通过ViewModel上的属性传递到PartialView 利用menu.ascx控件上的OutputCache指令来限制到数据库和资源文件的往返次数是有意义的。我的意图是用VaryByParam=none和VaryByCustom属性标记OutputCache指令,在global.asax中实现自定义安全查找
一些示例用户控制代码:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%@ OutputCache VaryByParam="none" VaryByCustom="customstring" %>
<ul>
<% var model = (IMyViewModel)Model;
foreach (var menu in model.Menus) { %>
<li><a href="<%= menu.href %>"><%= menu.Text %></a></li>
<% } %>
</ul>
这是一篇关于这个主题的有趣读物,在这里,我基本上通过母版页中的RenderAction方法来调用操作,该操作将从数据库和B中提取数据,然后缓存操作结果我想我已经找到了解决问题的合适方法
在我的具体ViewModel实现的菜单属性getter中,我正在编写代理代码,以返回到实例化控制器并请求菜单数据。这样,我可以在PartialView请求时动态创建菜单数据。如果PartialView来自OutputCache,则不会请求Menu属性
因此,我的IMyViewModel看起来有点像这样:
public interface IMyViewModel {
IEnumerable<Menu> Menus { get; }
///<summary>
/// A pointer back to the calling controller, which inherits from the abstract MyBaseController
///</summary>
MyBaseController Controller { get; set; }
}
public IEnumerable<Menu> Menus
{
get { return Controller.GetMenus(); }
}
公共接口IMyViewModel{
IEnumerable菜单{get;}
///
///指向调用控制器的指针,该控制器从抽象MyBaseController继承
///
MyBaseController控制器{get;set;}
}
我的菜单的具体实现有点像这样:
public interface IMyViewModel {
IEnumerable<Menu> Menus { get; }
///<summary>
/// A pointer back to the calling controller, which inherits from the abstract MyBaseController
///</summary>
MyBaseController Controller { get; set; }
}
public IEnumerable<Menu> Menus
{
get { return Controller.GetMenus(); }
}
公共IEnumerable菜单
{
获取{return Controller.GetMenus();}
}
评论?这是一个可行的解决方案吗?RenderAction仅在ASP.Net MVC futures中可用。。。我不确定我们是否想去那里。