2

I have an application that I am creating for multiple clients. The only differences between clients is the stlye sheets and a few graphics. I would like to have each client just use a different URL that will show the core content with different css like the following:

myapplication/client1/mycontroller/myaction
myapplication/client2/mycontroller/myaction

Can someone give me some pointers on how to accomplish is in .NET MVC 4?

Additional Info:
I am only deploying a single instance of this application. I want the application to dynamically use the proper style based on the "client" portion of my url.

Example. client1 has a green background, client2 has a blue background. Other than that client1 and client2 are running the same web application.

I have the following in my RouteConfig.cs file:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "client1",
            url: "client1/{controller}/{action}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "client2",
            url: "client2/{controller}/{action}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Update with Solution:

Here is the answer that I was looking for:

    routes.MapRoute(
    name: "client",
    url: "{client}/{controller}/{action}",
    defaults: new { controller = "Home", action = "Index" },
    constraints: new { client = "client1|client2" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
PJL
  • 23
  • 4
  • 2
    Please use the [asp.net-mvc] tag. The tag for [model-view-controller] says `For Microsoft ASP.NET MVC, please use [asp.net-mvc] tag instead.` – Ryan Gates Feb 23 '15 at 14:35
  • If you have the CSS files and images named the same for both clients, you'll just need to replace them for each client when deploying. Your URLs should always be relative to prevent breakage when the routes or base URL get changed. – krillgar Feb 23 '15 at 15:19

1 Answers1

3

Note: Not tested, just an idea. Couldn't format in a comment

You could name your css files with the clients url like:

test.com.styles.css

EDIT

I just realised that we can't access HttpContext in BundleConfig.cs as the Session is not created at this point.

So In you view (_Layout.cshtml) You will have generate the stylesheet name something like this:

        @{
            var host = HttpContext.Current.Request.Url.AbsoluteUri;
    // Will be http://www.test.com/AREA1


            var area = host.Split('/')[3];

    // SPLIT will give you AREA1

            var styleSheet = string.Format("{0}.styles.css", area);

    // This will give you Stylesheet Name:
    // So then you can use simple HTML to load the css file

var styleSheetUrl = string.Format("{0}{1}", "/LocationOfCssFile/", styleSheet);

        }

    <link rel="stylesheet" href="@styleSheetUrl">

You can get the correct portion of URL you require from here:

https://stackoverflow.com/a/593715/1910735

Community
  • 1
  • 1
Dawood Awan
  • 7,051
  • 10
  • 56
  • 119