c# - 不缓存 [ChildActionOnly] 操作在 ASP.NET MVC 中不起作用

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

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

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

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

@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 及其 View 是否应该位于 Shared 文件夹中?

最佳答案

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

关于c# - 不缓存 [ChildActionOnly] 操作在 ASP.NET MVC 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29001092/

相关文章:

c# - Dynamics C# 插件在运行时失败,但在调试时成功

c# - 调试停止时,VS2015 不会终止 IIS 进程

asp.net - 表单例份验证超时与 session 超时

asp.net - 回发后如何从 jQuery 模式对话框中获取文本框值?

c# - 如何在标题下方的 Accordion 中添加箭头图像而不是标题背景?

JQuery 不在 $(document).change() 上执行

c# - 编译器版本与 NET Framework 版本 - ASP.NET 应用程序的场景

c# - 将 native 非托管 C++ DLL 加载到托管 C# 应用程序会导致 DLL 输出垃圾

javascript - 使用 jquery 填充数组样式表单字段

c# - 用于在字符串中查找 URL 的正则表达式