views:

59

answers:

4

So I know there are various ways of doing, however I'd like to know the "proper" way of including a specific CSS dynamically based on the page I am on. I could include all of them within the site master, however I'm wondering if I could simply include them ONLY when I need it, by either evaluating URL or passing a value through the controller for display flag, or just include it within the content page (outside of the head tags)... I'd like to keep it clean and link them all through my site master, but I'd like to be able to evaluate the page I'm on before I include that CSS..... thoughts??

+1  A: 

No matter what its going to be something like this:

<% if( someCriteria ) { %>
   <stylesheet type="text/css" href="mypath" />
<%} %>

You could wrap it in a helper or whatever but I don't think there can be a best practice or "cleaner" way of doing something this simple.

" I could include all of them within the site master, however I'm wondering if I could simply include them ONLY when I need it"

Another way to look at this is CSS files are cached by the browser so you may as well include it once and be done with. Your visitors may have a slightly longer initial load time but if you keep your CSS files small it will be barely noticeable. There is very little performance benefit by making it dynamic.

jfar
Thanks... makes sense.... still new to the .NET scene.... as for the front-end load by having all the styles in 1 file, you pretty much hit the nail on the head about the initial load, which is why I'd rather break it down in smaller chunks... and multiple people can modify the files without having to worry about overlapping someone elses changes at the same time...
denisb
+1 for the second part of your answer.
JasCav
A: 

What I typically do is add a to the master in the to allow for pages to include things there. More often than not it is scripts rather than stylesheets, but it works for both.

I'd also vote for getting a Html helper in place to handle this so your developers don't need to care about exactly where the stylesheet is loaded from . . .

Wyatt Barnett
I always load it through helper - because it allows me to construct the path at runtime and it allows me to attach Caching and Compression filters to it, which I can also disable when testing (or changing CSS every 5 minutes).
mare
+1  A: 

If you're using the Spark view engine, you can use the once attribute on your css include. I personally just put everything on the site master and let the browser handle caching.

http://sparkviewengine.com/documentation/expressions#Conditionalattributeonce

Ryan
+1  A: 

Good, bad, or indifferent, the thing I have been doing for years with master pages is include a ContentPlaceHolder in the <head> section of the master page. I can then inject page-specific CSS through that, instead of cluttering my master page with alot of processing logic. I am doing the same thing with my ASP.NET MVC solutions.

Jonathan Bates
Exactly how I do it too.
Jamiec