Neilski
Neilski

Reputation: 4415

Dynamically Managing MVC Layouts

I have a small MVC web project where I want to be able to achieve the following:

  1. Select the base page layout and CSS/JavaScript based upon the active domain
  2. Optionally allow this base/default setting to be overridden at the start of the session.

To help achieve this I have created a layout object with the following properties:

public class PageLayout {
   public string Reference { get; set; }
   public string Domain { get; set; }
   public string LayoutPath { get; set; }
   public string CssPath { get; set; }
   public string JavaScriptPath { get; set; }
}

My idea being that at the start of the session, the URL will be checked for a layout parameter. For example:

http://www.{Domain}.com/tech

In this instance, the Pagelayout object with the Reference "tech" would be retrieved. If no parameter was found then the Page Layout object with its Domain property matching the active domain would be retrieved.

I have several questions regarding the right way to implement this:

  1. Where is the best place to implement this logic in MVC? The Session_Start method in Global.asax seems like a potential candidate
  2. I want to persist the retrieved PageLayout object across the whole session. I was going to add it to the Session state via some kind of management class.
  3. How do I make the Pagelayout data available to each page. I thought about creating a custom Controller and then adding it to the ViewBag (from the Session), so the master view could implement something like the following:

    @{ Layout = ViewBag.Pagelayout.LayoutPath; } ...

Are the better/cleaner/more appropriate mechanisms available to achieve what I need?

Upvotes: 0

Views: 572

Answers (1)

Nipun Ambastha
Nipun Ambastha

Reputation: 2573

Yes there are cleaner ways to do, like using some third party tool and to hook it your application.

You can take a look at this site, this is the latest that have been introduced recently http://razorc.net/

Also take a look at http://www.codeproject.com/Articles/32847/ASP-NET-MVC-Dynamic-Themes http://codeofrob.com/entries/dynamically-switching-between-master-pages-in-asp.net-mvc.html

Upvotes: 1

Related Questions