views:

74

answers:

2

We need to present the same site, with a different theme (and different data) depending on the "route" into the site.

  • www.example.com/Trade
  • www.example.com/Public

NB: These are not dynamic themes which the user can pick. Certain customers will be coming via a third party link which will always point them to one of the urls.

The value trade/public also needs to be used in queries from the UI to the database (to pull back varying data depending on the route into the site).

So what are my options?

Create custom view engine which uses the querystring (mvc route param) value to load the relevant master page.

In each controller action, grab the parameter (trade/public etc) and pass it off to the database queries.

public ActionResult List(string siteType){
    products.ListFor(siteType);
}

The problem here is having to alter every controller action to pass the querystring value through.

This also presents an issue with any route defined in global.asax having to accept the parameter.

I'm wondering if there's another way, perhaps some combination of a custom controller base and host name e.g. trade.example.com, public.example.com?

+1  A: 

How about this:

Create a base controller that all your controllers inherit from

Add a property: public string SiteType { get; protected set; }

Add an OnActionExecuting method to set it before any action is called, e.g.

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
 //use filterContext to set SiteType somehow, e.g. you can look at the URL or the route data
}
JonoW
+1  A: 

First define routing:

routes.MapRoute(
            "Default", // Route name
            "{siteType}", // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

and then use it in controller:

RouteData.Values["siteType"]
LukLed
Using this in combination with a custom base controller to retrieve the value does the trick! Thanks for the help
jonhilt