Master View/Child View Combination

Apr 4, 2011 at 5:22 PM


Perhaps I missed this in the documentation., but how do you define the setup for Master/Child view so that you do not have to have 2 separate files generated?

if I try to do it this way I get Duplicate resource definition 'MainCss'


<% List<String> cssFiles = new List<String>();

<%= Html.CompositeCssResource("MainCss",cssFiles)%>

<% List<String> cssFiles = new List<String>();        



<%= Html.CompositeCssResource("MainCss",cssFiles)%>
so if I change the second to "PageCss" it works fine but I get 2 css files on the page.
any advice would be appreciated
Apr 4, 2011 at 9:25 PM


You have not missed the documentation, the closest reference would be another discussion point (

 With WebForms, the feature was easy to implement, as there was a requirement to have an explicit content placeholder regardless; so you could collect all resources while the page is being processed, and then while rendering, spit out the combined resource file. With MVC, this isn't really an option as HTML fragments are rendered as they are traversed/processed. 

I have found the use of extension methods to handle this case well enough. Something similar to below (with meaningful names of course!)

public static class CustomXpediteExtensions
  private static readonly IEnumerable<String> SharedCss = new[]

  public static MvcHtmlString ExtendCompositeCssResource(this HtmlHelper htmlHelper, IEnumerable<String> resources)   
    return htmlHelper.CompositeCssResource(SharedCss.Concat(resources));  

If you have any suggestions on how to better handle this case, I would be happy to hear it; I thought about this for a while and never came up with a solution that didn't smell and this worked for me.