Jonas Stawski

Everything .NET and More

Dynamic CSS with ASP.NET MVC and ContentResult

The ContentResult class is of type ActionResult, which means it is used to return text content from Controller Actions. There are multiple uses for the ContentResult, and one use is to create CSS dynamically. One would simply point a style sheet to an action and the action would return the dynamically generated CSS. In my case I am developing a Web app where each client can have their own theme, which is set in the database. I tackled this requirement with the following code:

   1: public ActionResult CssStyle()
   2: {
   3:     string style = GetStyleFromDB(clientID);
   4:     if (!String.IsNullOrWhiteSpace(style))
   5:         return Content(style, "text/css");
   6:     return View();
   7: }
I would call this Action from the View like so:
   1: <link href="@Url.Action("CssStyle", "Home")" rel="stylesheet" type="text/css" />

 

Happy Programming (and styling)

Comments (2) -

Thanks Buddy, this is great!

Reply

Very Nice

Reply

Add comment

biuquote
Loading