Cache page without navigation bar. User login / logout system not updating at all (MVC)

I am trying to cache a page without a page navbar. When I cache the page everything 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 clicks the login button and logs in, the user is redirected to the same page (index) and no login occurs (username and logout button are not are displayed), the login and registration buttons are still displayed, this is a problem.

This is my code:

Home controller:

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

      

Is there anything I can do to prevent this?

+3


source to share


1 answer


I managed to find a solution using haim770's guidelines.

Solution using "Donut-Caching" ( https://github.com/moonpyk/mvcdonutcaching )

1. First I get "donut caching from NuGet packages".

2.I switched to the _layout.cshtml page to line: @Html.Partial("_LoginPartial")

with@Html.Action("partialView", true)

3.That I am creating an action inside an account controller named partialView that returns the view I wanted, for example:

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

      



4. After that I decorated the action returning the index page with

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

like this:

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

      

And you did, Thanks again to Chaim (Chaim).

0


source







All Articles