Master View/Child View Combination

Apr 4, 2011 at 5:22 PM

Hello,

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'

Site.Master

<% List<String> cssFiles = new List<String>();
       cssFiles.Add("/Content/2011.1.315/telerik.common.min.css");
       cssFiles.Add("/Content/2011.1.315/telerik.telerik.min.css");
       cssFiles.Add("/Content/cupertino/jquery-ui-1.8.7.custom.css");
       cssFiles.Add("/Content/fullcalendar.css");
       cssFiles.Add("/Content/default.css");
%>

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

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

cssFiles.Add("/Content/resources/reset.css");
cssFiles.Add("/Content/resources/style-2.css");
cssFiles.Add("/Content/resources/invalid.css");

 %> 

<%= 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

Hi,

You have not missed the documentation, the closest reference would be another discussion point (http://xpedite.codeplex.com/discussions/232004).

 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[]
    {
      "/Styles/SharedStyle1.css",
      "/Styles/SharedStyle2.css",
      "/Styles/SharedStyle3.css"
    };

  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.