Upload _Layout.cshtml information from database

Asked

Viewed 233 times

1

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

What’s the best way to do it?

I want to carry this only once, thought I’d even use and store it all in Session.

  • Wouldn’t it be better to make this data available in a Source in the application? Understand that this data is usually not changed frequently and are only system parameters.

1 answer

0

What’s the best way to do it?

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

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 of this configuration table is frequent, the cache is automatic.

Still, you can define the cache manually:

HttpContext.Cache["MetaDescription"] = "Alguma descrição";

Browser other questions tagged

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