Layout pages head:
<head>
<link href="@Url.Content("~/Content/themes/base/Site.css")"
rel="stylesheet" type="text/css" />
</head>
A View (AnotherView) from the application needs:
<link href="@Url.Content("~/Content/themes/base/AnotherPage.css")"
rel="stylesheet" type="text/css" />
and AnotherView has a partial view (AnotherPartial) which needs:
<link href="@Url.Content("~/Content/themes/base/AnotherPartial.css")"
rel="stylesheet" type="text/css" />
Question: How can we add these CSS files links AnotherView and AnotherPartial links to Layout head?
RenderSection is not a good idea because AnotherPage can have more than one Partials. Add all CSS to head is not useful because it will change dynamicaly (it depends on Anotherpages).
Layout:
View:
Update: basic example available at https://github.com/speier/mvcassetshelper
We are using the following implementation to add JS and CSS files into the layout page.
View or PartialView:
Layout page:
HtmlHelper extension:
Here is a NuGet plugin called Cassette, which among other things provides you the ability to reference scripts and styles in partials.
Though there are a number of configurations available for this plugin, which makes it highly flexible. Here is the simplest way of referring script or stylesheet files:
According to the documentation:
I had a similar problem, and ended up applying Kalman's excellent answer with the code below (not quite as neat, but arguably more expansible):
The project contains a static AssignAllResources method:
in the _layout page
and in the partial(s) and views