ASP.Net MVC与缓存数据库驱动的菜单 [英] ASP.Net MVC Database-driven menu with caching

查看:171
本文介绍了ASP.Net MVC与缓存数据库驱动的菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建我的网站的菜单。它需要满足下列要求

I am trying to create a menu for my website. It needs to meet the following requirements


  • 它必须是数据库驱动的,从数据库中提取数据,建立菜单结构

  • 需要高速缓存从数据库被拉扯的数据 - 我不想打DB为每个页面请求

目前,我有一个简单的例子运行,但我不知道如何整合缓存。我想我可能要返工我这样做对整个道路。在这里,它是:

At the moment, I have a simplistic example running, but I don't know how to integrate caching. I think I might have to rework the entire way I do this. Here it is:

我有一个 ProductMenuAttribute ,它拉的ViewData的从数据库中的数据,并将其存储:

I have a ProductMenuAttribute, which pulls the data from the DB, and stores it in the ViewData:

public class ProductMenuAttribute: FilterAttribute, IActionFilter
{
    #region IActionFilter Members

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (filterContext != null)
        {
            var context = filterContext.Result as ViewResult;
            if (context != null)
            {
                ProductsRepository repository = new ProductsRepository(Properties.Settings.Default.SqlConnectionString);

                context.ViewData.Add("ProductsList", repository.GetAllProductRanges());
            }
        }
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {

    }

    #endregion
}

在我的 Site.master母我拉将数据从的ViewData ,并用它来使我的菜单。这是我未排序菜单列表,这将会使用CSS设计的一个小片段。这里是code:

In my Site.master I pull the data from the ViewData and use it to render my menu. This is a small snippet in my un-ordered menu list, which gets styled using CSS. Here is the code:

            <li>
                <%= Html.ActionLink("Products", "Index", "Products")%>

                <%  IQueryable<ProductRange> productRanges = ViewData["ProductsList"] as IQueryable<ProductRange>; %>

                    <% if (productRanges != null)
                       { %>

                    <ul>
                        <% foreach (ProductRange range in productRanges) 
                           { %>   
                            <li><%= Html.ActionLink(range.Name, "RangeDetails", "Products", new { id = range.ID }, null)%></li>
                        <% } %>
                    </ul>

                    <% } %>
            </li>

然后我用装点在 [ProductMenu] attribuate每个控制器如下:

I then decorate each controller with the [ProductMenu] attribuate as follows:

[ProductMenu]
public class HomeController : BaseController
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        return View();
    }
}

现在,每当我的任何控制器上的动作被击中,在 OnActionExecuted 的方法在 ProductMenuAttribute 类将被调用,这将设置ViewData的,这最终会在用我的的Site.Master 以建立我从数据库中,我调用任何动作的这段时间菜单。

Now whenever any of the actions on my controller are hit, the OnActionExecuted method in the ProductMenuAttribute class will be called, which will set the ViewData, which will eventually be used on my Site.Master to build up my menu from the DB, which time I call any of the actions.

现在的问题是 - 我怎么添加缓存此方案?我不知道从哪里开始,有一种感觉的解决方案,我已经是不可缓存。

The problem now is - how do I add caching to this scenario?? I have no clue where to start, and have a feeling the solution I have is not cacheable.

推荐答案

我想我真正需要的是使用 Html.RenderAction()从MVC期货项目帮手扩展。

I think what I am really looking for is to use the Html.RenderAction() helper extension from the MVC Futures project.

的想法是使用上述呼叫的控制器,这将通过从DB提取数据生成HTML菜单结构上的动作。然后我还用简单的输出缓存到缓存中的数据了几分钟。

The idea is to use the above to call an action on a controller, which will generate the HTML menu structure by pulling data from the DB. I then also use simple Output Caching to cache the data for a few minutes..

这是我到目前为止发现达到我想要的最简单的方法。

This is the simplest approach I've found so far to achieve what I want.

编辑:菲尔哈克博客上讲述这个最近 - 的Html .RenderAction和Html.Action 。一个伟大的博客文章涵盖了我所有的具体需求,与所有的问题作出说明。

Phil Haack blogged about this recently - Html.RenderAction and Html.Action. A great blog post covering all my exact needs, with explanations of all the issues.

要获得缓存正常工作,我需要把我的 Html.RenderAction()电话里面的 ViewUserControl 与OutputCaching指令设置如下:

To get the caching to work correctly, I would need to put my Html.RenderAction() call inside a ViewUserControl with the OutputCaching directive set as follows:

<@ OutputCache Duration="100" VaryByParam="None" %>

然后我打电话以上 Html.RenderPartial(),瞧,它所有的作品!

这篇关于ASP.Net MVC与缓存数据库驱动的菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆