C# MVC5 - Using Outputcache

Asked

Viewed 914 times

1

I recently started studying the OutputCache and its uses due to a use in a corporate system.

I’m using the OutputCache to load all menus that a user (logged in) will have access to. However, I want that every login he reload this method again (action), regardless of whether the time (Duration) sold out or not...

Does anyone know if I can do this via override GetVaryByCustomString in the Global.asax? If yes, how?

Hugs!

1 answer

0


I don’t quite understand how you’re using the OutputCache in the Global.asax. Usually a decoration is made by Attribute in the Controller. In your case, it would be Action login:

    [OutputCache(Duration=int.MaxValue, VaryByParam="none")]
    public ActionResult Login()
    {
        /* Passos do seu método de login aqui */
    }

Or, still, you can use a Action to mount the menu and place the OutputCache in it. I teach that in this answer:

    [ChildActionOnly]
    [OutputCache(Duration=int.MaxValue, VaryByParam="none")]
    public ActionResult Menu()
    {
        /* Monte seu menu aqui */
    }

EDIT

I understood the doubt. In this case, I think it would be nice if you force the cache to expire Action of Logout:

public ActionResult Logout()
{
    var url = Url.Action("ControllerDeLogin", "Login");
    HttpResponse.RemoveOutputCacheItem(url);
    // Coloque o restante da lógica aqui.
}
  • So that’s what I’m doing. But what I want is that if I change the access of a user’s menu, (I take the access to the 'configuration' menu for example), I want that when logging out, and later the login, my 'actionresult Menu()' will run again, regardless of whether Duration has run out or not...

  • @Antôniofilho I updated the answer.

  • So, I imagined this... I’ll end up putting in Login, in [Get]. This option is really the only one?

  • It’s by Custom String, but it serves more for a global scope, which is not your case.

  • Sure, I get it. But that’s right! So you don’t even have to use 'override Getvarybycustomstring' in 'Global.asax'... I’ve seen a lot of people talk about using this method, but theoretically it doesn’t even make sense...

  • Exactly, but not everyone is looking for a more suitable solution. I’m glad it worked!

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.