Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Donut caching _Layout with mvcdonutcaching ASP.NET MVC

Tags:

In my ASP.NET MVC project, I have a login submenu in the navigation menu of my shared _Layout.cshtml file, displaying user info if the user is logged in, or signup/login options if not. The login submenu is a partial view in my shared folder named _LoginPartial:

@using Microsoft.AspNet.Identity @if (Request.IsAuthenticated) {     //display <ul> with user profile settings, omitted for brevity } else {     //display <ul> to signup/login, omitted for brevity     } 

While I heavily cache several actions of various controllers, I want to implement donut caching on _Layout so that _LoginPartial does not get cached, for obvious reasons. I'm using the mvcdonutcaching library to accomplish this (suggested in this answer) which provides some overloads of @Html.Action that have an additional bool excludeFromParentCache property.

As such, I created a LayoutController with a UserAuth action which returns _LoginPartial:

public class LayoutController : Controller {     [ChildActionOnly]     public ActionResult UserAuth()     {         return PartialView("_LoginPartial");     } }  

..And in my _Layout file, where I want _LoginPartial to appear, I call the mvcdonutcaching Html.Action overload as such:

@Html.Action("UserAuth", "Layout", true) 

To test this, I have set an OutputCache with a long duration on the Index action of my FAQController, but if I follow these steps:

  • login
  • navigate to /faq
  • logout
  • navigate to /faq

/faq still shows me as logged in.

What am I missing here? This is mvcdonutcaching's output in the actual HTML:

<!--Donut#3ED0C02DC8A537BA39C854B0D03E9A954F9FD01409A5E10C6C623D32512359E90086702A97EB36055229506A07D84CC1F6F7D1BF0A230DE5E87423363C24CA8D8C93D671FF398054DA29A7594CE2B8E939195C563004CE281D76DD838DB25198FCCCEC694F80885B86E611E2C5D9DE0C0B9B67432AD021FF581FD4A652C611D62B12C4C3A327E917940F333B56268D530831CCA617AF126AA0F809E5FBF1AB3C4231B11851F0BC73ED1A0B43A81AFF7B9FB081B7DF4B90712965596411627ECABD9DDFD519438910DBFCB94A22C216B1C3ABDBB5FC5E436E838505E6B56698E37CDF09A47CEAB5A3E3269FA326EA9191142954445BC92CE50248A0F7B964764C6E6768A92C31E55AE07AF230ECB7B8E8A5B048A82CD035095D84BDDA6336ED7805BB89CAB8C92AF1E4FA7971DF92CC2C2BC68CF605B594191DD55BDAF4E90D6451EF10FA18140B4201D16071052D5CC2B9490BFA00B2DDC622ED22CA1F8DB75E5F30E830B9D7B13778BC6E63EC49745AC037A4009A4CF05749568BD4D3DB8AE4A1E08024#-->   <ul class="nav navbar-nav navbar-right">     <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> </ul> <!--EndDonut--> 

Update: I have also tried moving the menu in _LoginPartial to a partial view residing in the views of LayoutController instead - the problem persists.

like image 732
trashr0x Avatar asked Jan 20 '15 00:01

trashr0x


1 Answers

I ran into the exact same thing but solved it after noticing that I had inadvertently used [OutputCache] instead of [DonutOutputCache]!

User error. Works in _Layout beautifully. Please double-check that you are using the proper [DonutOutputCache] attribute.

like image 85
user61307 Avatar answered Sep 29 '22 22:09

user61307