ASP.Net MVC Master Page architecture advice and dynamic CSS / JavaScript references?
I am moving into the ASP.Net MVC realm and need some thoughts / advice on architecture. In my current ASP.Net web forms application I am using a nested master page approach. I have a Main site master that exposes all of the common functionality and layout elements for the site. I then have a set of other master pages that inherit from the Main master the expose specific functionality for those sections. Examples of these nested master pages are: a master page for modal popup windows and a desktop type layout for most of th开发者_开发技巧e site.
Part of my architecture for the application is that CSS and JavaScript /JQuery reverences could be inserted into the masters or inherited aspx pages dynamically. I created a UI assembly that took care of building the references out. Here is some code from that project:
/// <summary>
/// This is the class model for a css reference
/// </summary>
public class CssReference
{
public string Href { get; set; }
public TypeEnum Type { get; set; }
public enum TypeEnum
{
TextCss
}
}
//This is the collection in the Main Master
public List<CssReference> CssReferences
{
get;set;
}
///this is how we would add to it from another inherited page or master
Master.CssReferences.AddRange(DefaultCssReferences());
I exposed a public property in my main master page that was a List (UIReference refers to wither the CSS or JavaScript reference types). This way from my inherited master or actual aspx page I could add to that collection any other JS or CSS references.
My question is, has anyone else done something similar to this in ASP.Net MVC or is there a better way of doing it?
I would probably go about this by having a BaseController class that all your controllers inheirt (inheriting the regular Controller class) then in there you can set your CssReferences to a property of the ViewBag.
(Post about ViewBag Usage: http://davidhayden.com/blog/dave/archive/2011/01/19/ViewBagAspNetMvc3.aspx)
Just depends on where you need to set the References.
On a side note, MVC3 with Razor allows for nesting of Layouts in a similar way to ASP.NET Web Forms.
_layout.cshtml has a RenderBody() function
MyInnerLayout.cshtml sets its Layout to _layout.cshtml and all its HTML is then rendered into where _layout.cshtml has its RenderBody() function
精彩评论