asp.net mvc - Passing variable into @Styles.Render function -
with razor, rendering specific bundle of stylesheets done with:
@styles.render("~/content/css")
this refers bundleconfig
file has line:
bundles.add(new stylebundle("~/content/css").include("~/content/site.css"));
... pointing site.css
file, inside content
folder.
i wanted set variable (i've tried session variable) this:
session["csstheme"] = "~/content/css";
so put in styles.render
function, this:
@styles.render(@session["csstheme"])
but gets error of invalid arguments.
i wanted change session variable value (to style bundle) , way change css style of web application.
so, how can pass edited variable styles.render
function?
first, session
dynamic, meaning can hold type inside. when pull out value, it's technically of type object
. styles.render
expects parameter of type string
, need cast value string
first:
@styles.render(@session["csstheme"] string)
then, there's problem potentially receive null value if either session variable not set @ all, or has been set other string value can't converted string. so, compensate that, should provide fallback nulls:
@styles.render(@session["csstheme"] string ?? "~/content/css")
now, either use whatever in session variable, or "~/content/css" last resort. bear in mind though, still pretty brittle. if session["csstheme"]
set string, not formatted bundle reference, you'll still error, , runtime error @ that, ugly. ideally, should have sort of value sanitization routine run session["csstheme"]
through first before passing styles.render
.
Comments
Post a Comment