imagine kitty magazine

Skip to main content
  • Categories

    • America
    • ASP.NET
    • Bible tags
    • C#
    • Christianity
    • Classic ASP
    • Firearms
    • Humor
    • LINQ
    • MVC
    • Programming
    • Random
    • Trackbacks
    • Web design
    • Web standards
    • Why I am the way I am
    • Wordpress
  • Links

    • ASP.NET Resources
    • Bible Dude
    • Cake Wrecks
    • Church Communications Pro
    • Creating Passionate Users
    • Dossy’s Blog
    • Gary Turner’s html & css workshop
    • id Projections
    • Information Pollination
    • SystemDotWeb
    • The Crazy Rants of Samantha Burns
    • The Nice Jewish Website
    • The Sneeze
    • Tyssen Design
  • Pages

    • Bible verse tags 2.0
    • Privacy Policy
    • Steps to building a proper web page
  • Info

    • home
    • Log in
    • contact mark
    • blogs that link here*
    • valid xhtml
    • valid css
    • valid rss2
    • rss feed
Sigarms P229

spam this

« Adding links to all pages on a site using CSS
Dynamic Javascript using ASP.NET MVC »

Dynamic CSS using ASP.NET MVC

I’ve been considering the usefulness of adding a CSS management system to my CMS that I’ve been working on. Here are the steps I’ve taken to implement it.

  1. Create a new MVC applictaion
  2. Open /Views/Shared/Site.Master
    1. <link href=”/Css/Site.css” rel=”stylesheet” type=”text/css” />
    2. This doesn’t point to anything yet
  3. Open Global.asax
    1. Add a MapRoute
      1. routes.MapRoute(“Css”, “Css/{cssFile}.css”, new { controller = “Css”, action = “Index”, cssFile = “Site” });
      2. Now /Css/Site.css points to Css/Index
  4. Add a class Css.cs to Models
    1. Add public string Body { get; set; }
    2. Add public string Width { get; set; }
    3. Add public string Background { get; set; }
  5. Add definitions as desired
  6. Add a view for CssController/Index
    1. Make it strongly typed with CSScontrol.Models.Css as the model
  7. Change contenttype in page directive of Views/Css/Index.aspx to “text/css”
  8. That should do it.

I’m not sure that I’m convinced of the usefulness of this idea but it’s possible and easy to implement in your ASP.NET MVC project. The possibility of using variables in a CSS file is intriguing.

You may download a sample of the MVC2 project here.MVC3 project here. Let me know if this is a good idea, a horrible idea or just a bit interesting.

Edit – I’ve added a Response.Filter to this to remove extra spaces, tabs, carriage returns/new lines, final semicolons and comments. It brought the 4.96kb file down to 2.74kb. It can be enabled/disbled in Web.config using appSettings key CompressCSS (bool)

This entry was posted on 26Feb10 @ 0946 and is filed under ASP.NET, C#, MVC, Programming. You can follow any responses to this entry through the RSS 2.0 feed. You can this page, leave a response or trackback from your own site.

5 Responses to “Dynamic CSS using ASP.NET MVC”

On 8Mar10 at 1104 Mark said:

Mark

I’ve added a few modifications to the file but I’m still not sure of the actual usefulness of this idea. Perhaps it is an answer to a question that should never have been asked.

On 29Mar10 at 1623 Iain said:

I think this is a good idea, and is particularly handy for dynamic css in load balanced environments. Good work :-)

On 10Apr10 at 0306 cShift3 said:

I’m still not sure of the actual usefulness of this idea.

Well, this way you can improve maintainability of the css file and open up for more customization at the same time.

I would also wrap the css inside of a style tag to get the intellisense
and remove the tag in the CSSCompress filter.

Nice post!

On 10Apr10 at 0807 Mark said:

Mark

Yes, it does make sense to make use of intellisense. I should have thought of that. Thanks. :)

I should probably also move the “if (Convert.ToBoolean(System.Configuration.ConfigurationSettings.AppSettings["CompressCSS"]) == true)” to CSSCompressor as well. That way it can still strip the style tags even with CompressCSS set to false.

On 15Aug10 at 1401 Mark said:

Mark

Thinking about it more, it seems to fit better with Javascript than CSS. Targetting dynamic content without adding <script> content to the head of a page by allowing the .js file to be dynamic.

Now that makes more sense to me.

Leave a comment

Sponsored links

468x60-1 email address
Google
Custom Search

Amazon Wishlist

Hello, friends! I've decided to put my Amazon wishlist online. If you feel nice today you can purchase one of the items listed and it will be shipped to my door. My birthday is May 13th and I will gladly accept gifts for any Christian or Jewish holiday. Thank you for your support.

Site search and links

I'm a friend of Israel

234x60

Open Trackback Alliance Logo


Widgetize!
WWW is deprecated
no-WWW class B
free email addresses
*blogs that link here powered by Technorati

©2013 imaginekittymagazine