396

In a .NET MVC4 project how does @Styles.Render works?

I mean, in @Styles.Render("~/Content/css") which file is it calling?

I dont have a file or a folder called "css" inside my Content folder.

1

8 Answers 8

466

It's calling the files included in that particular bundle which is declared inside the BundleConfig class in the App_Start folder.

In that particular case The call to @Styles.Render("~/Content/css") is calling "~/Content/site.css".

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); 
Sign up to request clarification or add additional context in comments.

6 Comments

One thing to know is that it will not add a .css file that is already minimized to the bundle. Example: it does not work with bootstrap.min.js, only with bootstrap.js. I hope this can help others.
This is talking about styles, not scripts. If you want to use bootstrap.min.js, just create a bundle like this: bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include( "~/Scripts/bootstrap.min.js"));
@codea I'm not sure what is your set-up, but by default bundler will take *.min.* over *.* files.
@skmasq, at the time of writing these lines, I was using VS2013. Things may have changed until now. Thanks for mentioning that :)
I dont get it....why go to all the trouble to create bundles and add those paths to these crazy classes in MVC when you can simply add a CSS <link> to the file in your web page? If you add all your CSS links to your style sheets in say a layout file or a partial view you can manage them in one simple place, as well. This is also bad design to hard code Style paths like that, as you can no longer create CSS skins which was the whole purpose of the CSS system when it was designs 20 years ago.
|
34

Watch out for case sensitivity. If you have a file

/Content/bootstrap.css

and you redirect in your Bundle.config to

.Include("~/Content/Bootstrap.css")

it will not load the css.

2 Comments

Also: The second include is spelled differently.
is there support for sass/less-files, too?
18

A bit late to the party. But it seems like no one has mentioned
bundling & minification of StyleBundle, so..

@Styles.Render("~/Content/css") 

calls in Application_Start():

BundleConfig.RegisterBundles(BundleTable.Bundles); 

which in turn calls

public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new StyleBundle("~/Content/css").Include( "~/Content/bootstrap.css", "~/Content/Site.css")); } 

RegisterBundles() effectively combines & minifies bootstrap.css & Site.css
into a single file,

<link href="/Content/css?v=omEnf6XKhDfHpwdllcEwzSIFQajQQLOQweh_aX9VVWY1" rel="stylesheet"> 

But..

<system.web> <compilation debug="false" targetFramework="4.6.1" /> </system.web> 

only when debug is set to false in Web.config.
Otherwise bootstrap.css & Site.css will be served individually.
Not bundled, nor minified:

<link href="/Content/bootstrap.css" rel="stylesheet"> <link href="/Content/Site.css" rel="stylesheet"> 

Comments

0

src="@url.content("~/Folderpath/*.css")" should render styles

Comments

0

As defined in App_start.BundleConfig, it's just calling

bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css")); 

Nothing happens even if you remove that section.

Comments

0

Polo I wouldn't use Bundles in MVC for multiple reasons. It doesn't work in your case because you have to set up a custom BundleConfig class in your Apps_Start folder. This makes no sense when you can simple add a style in the head of your html like so:

<link rel="stylesheet" href="~/Content/bootstrap.css" /> <link rel="stylesheet" href="~/Content/bootstrap.theme.css" /> 

You can also add these to a Layout.cshtml or partial class that's called from all your views and dropped into each page. If your styles change, you can easily change the name and path without having to recompile.

Adding hard-coded links to CSS in a class breaks with the whole purpose of separation of the UI and design from the application model, as well. You also don't want hard coded style sheet paths managed in c# because you can no longer build "skins" or separate style models for say different devices, themes, etc. like so:

<link rel="stylesheet" href="~/UI/Skins/skin1/base.css" /> <link rel="stylesheet" href="~/UI/Skins/skin2/base.css" /> 

Using this system and Razor you can now switch out the Skin Path from a database or user setting and change the whole design of your website by just changing the path dynamically.

The whole purpose of CSS 15 years ago was to develop both user-controlled and application-controlled style sheet "skins" for sites so you could switch out the UI look and feel separate from the application and repurpose the content independent of the data structure.....for example a printable version, mobile, audio version, raw xml, etc.

By moving back now to this "old-fashioned", hard-coded path system using C# classes, rigid styles like Bootstrap, and merging the themes of sites with application code, we have gone backwards again to how websites were built in 1998.

4 Comments

So, to heck with minification then? :s / :(
Yes. Why are developers in 2019 minimizing css and javascript but then building API's like Angular and others that send megabytes of unnecessary ECMAScript (Javascript) to clients? We used to send less code or compressed code to customers with limited bandwidth so they could get the code when they were constrained by bandwidth.... i.e. 14k baud modems. We have 5G coming so code compression like gif compression or minification isnt needed. Yet we have gone backwards sending huge chucks of scripts to clients as a result. So why minimize anything?
Because we should be sending as little over the wire as we possible can? I certainly try to keep things to a minimum.
If nothing else, minify for eco-friendliness since less data on the wiring means less electricity, less equipment, less storage, less everything and more green centric consciouness
0

I did all things necessary to add bundling to an MVC 3 web (I'm new to the existing solution). Styles.Render didn't work for me. I finally discovered I was simply missing a colon. In a master page: <%: Styles.Render("~/Content/Css") %> I'm still confused about why (on the same page) <% Html.RenderPartial("LogOnUserControl"); %> works without the colon.

Comments

0

Set this to False on your web.config

<compilation debug="false" targetFramework="4.6.1" /> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.