Passing Configuration Data to the Master Page in ASP.NET MVC

aweiker

One of the common activities that I have found myself doing lately whenever I create a new project is to identify a way of loading configuration information and customizing the behavior of the Master Page. The way I initially went about solving this problem was to create a base class and have all of my Page ViewModel’s inherit from it. This caused two different things that I had to constantly maintain, making sure all of my ViewModel’s inherited from this MasterViewModel and then remembering to set the data every time a ViewResult is returned in an Action.

    public class MasterViewModel : IMasterViewModel
    {
        public string SiteName { get; set; }
    }

I was able to take care of the second concern by simply creating a base controller that all of my controllers would inherit from and then override View(string viewName, string masterName, object model) { … } so that the settings are automatically injected into the model.

        protected override ViewResult View(string viewName, string masterName, object model)
        {
            ((MasterViewModel)model).SiteName = "Example";
            return base.View(viewName, masterName, model);
        }

In order to get around the smell of forcing my ViewModel’s to inherit from a base class I put the MasterViewModel into the ViewData dictionary that is returned in the ViewResult.

        protected override ViewResult View(string viewName, string masterName, object model)
        {
            ViewResult result = base.View(viewName, masterName, model);

            result.ViewData[Data.Site] = _blogConfiguration.Configuration;

            return result;
        }

Once I had this in place, I created a new ViewMasterPage with a property that exposed the data that was put into ViewData. This then allowed me to have a strongly typed referenced from the Master Page.

<%@ Import Namespace="Azure.Domain.Models"%>
<%@ Master Language="C#" Inherits="Azure.Web.Views.ViewMasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title><%= SiteConfiguration.Name %><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.1.min.js" type="text/javascript" language="javascript"></script>
    <script src="http://ajax.microsoft.com/ajax/jQuery.Validate/1.6/jQuery.Validate.min.js" type="text/javascript" language="javascript"></script>
    <asp:ContentPlaceHolder ID="HeaderContent" runat="server" />
</head>
 
<body>
    <div id="header">
        <h1><%= SiteConfiguration.Name %></h1>
    </div>
    
    <div id="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server" />
    </div>

    <div id="footer">
        ©<%= DateTime.UtcNow.Year %> <%= SiteConfiguration.Owner %>
    </div>
</body>
</html>

All examples are taken from the work I have been doing as a part of creating my own blog engine on Azure. You can track my progress and grab source code on my AzureBlog project site on CodePlex.

Tuesday, February 02, 2010 Comments
blog comments powered by Disqus