【问题标题】:Not caching [ChildActionOnly] action not working in ASP.NET MVC不缓存 [ChildActionOnly] 操作在 ASP.NET MVC 中不起作用
【发布时间】:2015-03-12 02:52:33
【问题描述】:

在我的 ASP.NET MVC 应用程序中,我有一个带有 Index 方法的 MenuController,并用 [ChildActionOnly] 装饰:

public class MenuController : Controller
{
    [OutputCache(Location=OutputCacheLocation.None, Duration=0, NoStore=true)]
    [ChildActionOnly]
    public PartialViewResult Index()
    {
        return PartialView();
    }
}

Index.cshtml 部分视图只是根据用户是否登录返回登录/注销导航菜单:

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("logoff", "account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
        @Html.AntiForgeryToken()
        <ul class="nav navbar-nav navbar-right">
            <li class="dropdown">
                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-at"></i> @User.Identity.GetUserName() <span class="caret"></span></a>
                <ul class="dropdown-menu" role="menu">
                    <li><a href="~/manage" class="dropdown" title="Manage your account"><i class="fa fa-user fa-lg"></i> Account</a></li>
                    <li class="divider"></li>
                    <li><a href="javascript:document.getElementById('logoutForm').submit()" title="Log out"><i class="fa fa-sign-out fa-lg"></i> Log out</a></li>
                </ul>
            </li>
        </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Sign up", "register", "account", routeValues: null, htmlAttributes: new { @class = "btn btn-signup", id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "login", "account", routeValues: new { returnUrl = @Request.Path }, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
}

我是从我共享的_Layout.cshtml 文件中调用它的:

@{ Html.RenderAction("Index", "Menu"); }

我想要完成的是永远不会缓存该特定菜单项,这样就不会缓存任何用户信息。但是对于发生缓存的页面,例如我的HomeController

public class HomeController : Controller
{
    [OutputCache(CacheProfile = "OneHour", Location=OutputCacheLocation.Server, VaryByParam = "None")]
    public async Task<ActionResult> Index()
    {
        //..some code
        return View(model);
    }

    [ChildActionOnly]
    [OutputCache(Duration = 86400, VaryByParam = "None")]
    public PartialViewResult Other()
    {
        //some code..
        return PartialView(otherModel);
    }
}

...解决方案不起作用:如果我转到未缓存的页面,导航到注销,然后返回/Home/Index,菜单仍显示我已登录。

我在这里缺少什么? MenuController 和它的视图是否应该在 Shared 文件夹中?

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 outputcache


    【解决方案1】:

    [ChildActionOnly][OutputCache(/*with anything other than Duration*/)] 在框架源代码中的实现非常有限,您必须使用: OutputCacheAttribute 基类实现自己的缓存属性类,或者在用户关闭页面时创建自动注销或者别的什么,这就是我现在能想到的。

    【讨论】:

      猜你喜欢
      • 2016-09-26
      • 2023-03-22
      • 1970-01-01
      • 2018-04-29
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 2010-12-06
      • 2012-06-03
      相关资源
      最近更新 更多