Update only a property of an object using Outputcache in Actionresult

Asked

Viewed 49 times

1

Hello, everybody.

I’ve got a problem I don’t even know if there’s a solution like this.

I have an Actionresult that returns the contents of a chart in Jquery.

The result of this chart will always need to be stored in a cache so that the page is not heavy.

Only a property of this model that needs to return true or false depending on whether the user is logged in or not.

For it to update correctly I would need to remove ONLY THIS PROPERTY from the cache

[OutputCache(Duration = 3600, VaryByCustom ="none")]
public ActionResult Index()
{
    GraficosModel model = geradorGraficos.GerarGrafico();

    if (this.UsuarioPesquisa != null)
    {
        model.usuarioLogado = true;               
    }
    else
    {
        model.usuarioLogado = false;
    }

    return View(model);
}

This 'model.user' property would have to be outside the cache. Is there any way to do this? Because it is always bringing false.

  • Why should you put the Logged in user into an object? Session would not solve?

  • the way the application was made so is already this way

1 answer

0

Rafaela, as this property is inside the answer, all content will be in the cache.

Try to change this part of your code:

[OutputCache(Duration = 3600, VaryByCustom ="none")]

To:

[OutputCache(Duration = 3600, VaryByCustom ="UsuarioLogado")]

and then change your Global.asax to:

public override string GetVaryByCustomString(HttpContext context, string arg) 
{
    return arg.ToLower() == "UsuarioLogado" ? User.Identity.Name : string.Empty;
}

With this custom parameter for the Outputcache attribute, we can have a cached page when there is no authenticated user and a page cached by the User.

Browser other questions tagged

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