Sunday 15 March 2015

html - how to Load CSS dynamically -



html - how to Load CSS dynamically -

in mvc application want apply themes. so, trying load css files bundleconfig beingness initialized in global.asax on app.start method() like

bundleconfig.registerbundles(bundletable.bundles);

however, want load css dynamically alter display style(theme) of page on drop downwards list or link button.

how can ? have tried write in basecontroller , there phone call 'registerbundles' method not working.

any help on appreciated.

a solution dynamic css link theme css entry in markup mvc controller action, passing in theme/customer id, e.q.:

<link rel="stylesheet/less" type="text/css" href='@url.action("themecss","render", new { id = model.accountid})' />

and themecss action method homecoming content so:

[httpget] [outputcache(nostore = true, duration = 0, varybyparam = "none")] public actionresult themecss(int id) //accountid { business relationship account = db.getinstance().accounts.firstordefault(a => a.accountid == id); accountsite site = account.accountsite; string col1, col2, col3, col4; if (site.theme == null) //custom colour theme { col1 = site.themecolour1; col2 = site.themecolour2; col3 = site.themecolour3; col4 = site.themecolour4; } else { col1 = site.theme.primarycolour.substring(1); col2 = site.theme.secondarycolour.substring(1); col3 = site.theme.othercolours.split(',')[0].substring(1); col4 = site.theme.othercolours.split(',')[1].substring(1); } string lessfile = "custom"; string less = system.io.file.readalltext(server.mappath("~/content/render/themes/" + lessfile + ".less")); less = regex.replace(less, @"(\d){6}", delegate(match match) { switch (match.groups[1].value) { case "1": homecoming col1 ?? "e6e6e6"; case "2": homecoming col2 ?? "b1b1b1"; case "3": homecoming col3 ?? "333333"; default: //case "4": homecoming col4 ?? "ffffff"; } }); homecoming new contentresult() { content = less, contenttype = "text/css" }; }

the downside of solution compared bundling lose out on css minification. if theme going fixed per accountid optimise caching changing outputcache attribute vary id parameter.

html css asp.net-mvc

No comments:

Post a Comment