3

I'm trying to cache a page without the navbar of the page. When i cache the page its all works fine but I get unwanted behavior.

Explanation:

When I cache the index page for example, the navbar is also cached so if the user press the log-in button and log-on, the user redirect to the same page (Index) and the log-in doesn't take affect (the user name and the log out button doesn't appear), the log-in and register buttons still shows, its a problem.

This is my code:

Home Controller:

public class HomeController : Controller
{
    [OutputCache(Duration=(60*60))]
    public ActionResult Index()
    {
        return View();
    }
    // ...
}

Can I do Vary by something to prevent it ?

Ron
  • 1,744
  • 6
  • 27
  • 53
  • 2
    Use Donut-Caching solution like https://github.com/moonpyk/mvcdonutcaching – haim770 Jan 01 '15 at 16:08
  • @haim770 thanks for the help but I need to NOT cache a partial view from my view, not html.action, There are any workaround ? – Ron Jan 01 '15 at 17:27
  • 1
    You'll have to exclude the navigation bar from the caching process somehow. The other approach you can try is to write some Javascript (on the client side) to show/hide the relevant parts based on the current user authentication state. – haim770 Jan 01 '15 at 17:30
  • So the question is how can I use the @Html.Action approach (in order to use the donut cache) for the rendering of the partial view of the log-in ? – Ron Jan 01 '15 at 17:36
  • 1
    Yes. You can see some examples in my first link. – haim770 Jan 01 '15 at 17:52

1 Answers1

0

I manage to find the solution using "haim770" guidelines.

The solution using the "Donut-Caching" (https://github.com/moonpyk/mvcdonutcaching)

1.first I get the "Donut Caching from the NuGet Packages.

2.I switched in the _layout.cshtml page the line : @Html.Partial("_LoginPartial") with @Html.Action("partialView", true)

3.Than I build an Action inside the Account controller called "partialView" that return the view I wanted, like this :

public ActionResult partialView()
    {
        return PartialView("_LoginPartial");
    }

4.After it I decorated the Action that return the index page with

[DonutOutputCache(Duration=(60*60))]

like this:

  [DonutOutputCache(Duration=(60*60))]
    public ActionResult Index()
    {
        return View();
    }

And you done, Thanks again to Haim(Chaim).

Ron
  • 1,744
  • 6
  • 27
  • 53