Dynamic User Customized Layout/Theme
I have a project about dynamic theming. I have 4 _Layout.cshtml (_Layout1, _Layout2, _Layout3, _Layout4) . When user saves the layout in db, I can get it to ViewStart.cshtml like below
@using Microsoft.EntityFrameworkCore
@using TestDynamicLayout.Data
@inject ApplicationDbContext _context;
@inject IHttpContextAccessor httpContext;
@{
var host = httpContext.HttpContext.Request.Host.Host;
subDomain = host.Split('.')[0];
var layout= await _context.LayoutPages.FirstOrDefaultAsync(x => x.SubDomainName == subDomain);
Layout = "~/Views/Shared/" + layout.Name;
}
But this code will take the subdomain and go to db to get the layout name with every request I think. What is the best practice for performance and traffic heavy sites with theming?
I tried also above code with Session
@using Microsoft.EntityFrameworkCore
@using TestDynamicLayout.Data
@inject ApplicationDbContext _context;
@inject IHttpContextAccessor httpContext;
@{
var layoutName = httpContext.HttpContext.Session.GetString("Layout");
if (string.IsNullOrEmpty(layoutName))
{
var host = httpContext.HttpContext.Request.Host.Host;
var subDomain = host.Split('.')[0];
layoutName = await _context.LayoutPages.FirstOrDefaultAsync(x => x.SubDomain == subdomain);
httpContext.HttpContext.Session.SetString("Layout", layoutName);
}
Layout = "~/Views/Shared/" + layoutName;
}
If I use this one how much time I shoul set the session timeout and if admin changes the layout again how can I set it again to new session for end users?
Or any other suggestion other than _ViewStart?
Thanks.