Upload information _Layout.cshtml database welcome

1

I want to load the information in _Layout.cshtml as registered in the database, for example, @ViewBag.MetaDescription , @ViewBag.MetaKeywords , @ViewBag.Title and other information also coming from the database.

What is the best way to do this?

I want to load this only once, I thought about even using and storing it all in Session .

    
asked by anonymous 04.05.2016 / 22:01

1 answer

0
  

What better way to do this?

Doing nothing. ASP.NET MVC has a property HttpContext.Cache that automatically caches for you. You just need to set the following in your common Controller :

public class Controller : System.Web.Mvc.Controller
{
    protected readonly SeuProjetoContext context = new SeuProjetoContext();

    protected override void Initialize(RequestContext requestContext)
    {
        base.Initialize(requestContext);
        var configuracao = context.Configuracoes.FirstOrDefault();
        ViewBag.MetaDescription = configuracao.MetaDescription;
        ViewBag.MetaKeywords = configuracao.MetaDescription;
        ViewBag.Title = // defina Title aqui
    }
}

As the load on this settings table is frequent, cache is automatic.

Still, you can manually set cache :

HttpContext.Cache["MetaDescription"] = "Alguma descrição";
    
09.09.2016 / 17:59