Search code examples
asp.net-mvcdecouplingcode-separation

.net mvc single admin section multiple sites


Desired outcome:

4 customer facing sites as separated projects 1 set of admin code used by all four sites

My current setup is 4 Projects:

  • Core for models/entities

  • Data for repositories

  • Controllers for, well, controllers

  • Web for views

Now, the Web project contains views for admin and customer facing sides. I'm wanting to split that into an admin project and 4 'customer' projects.

I've google with little success about referencing a web app from a web app. I've read about areas on haacked.com, but that doesn't seem quite right. I don't particularly want to embed views in a dll, since that would be a pain while debugging/coding markup. Obviously I don't want multiple copies of the code.

Can anyone point out or suggest possible ways to do this? I'm a bit stumped.


Solution

  • If the reason behind splitting the project into 4 webprojects is that customers have different views try this:

    Have 1 webproject and use a subfolder for views that differ.

    • Views/Home/About.aspx (used if no matching subfolder found )
    • Views/Home/Customer1/About.aspx
    • Views/Home/Customer2/About.aspx

    You can have a custom viewengine that decides which view it should render. Each installation of the web has a customer defined in the web.config.

    public class MultiTennantWebFormViewEngine : WebFormViewEngine
    {
      private static string[] LocalViewFormats = 
    
       new string[] {
           "~/Views/{1}/" +  ApplicationConfiguration.CustomerName + "/{0}.aspx",
        "~/Views/{1}/{0}.aspx",
        "~/Views/{1}/" + ApplicationConfiguration.CustomerName  +   "/{0}.ascx",
        "~/Views/{1}/{0}.ascx",
        "~/Views/Shared/{0}.aspx",
        "~/Views/Shared/{0}.ascx"
    };
    
      public LocalizationWebFormViewEngine()
      {      
        base.ViewLocationFormats = LocalViewFormats;
         base.PartialViewLocationFormats = LocalViewFormats;
         base.MasterLocationFormats = new string[] {
    
              "~/Views/{1}/" +  ApplicationConfiguration.CustomerName  + "/{0}.master",
              "~/Views/{1}/{0}.master",
               "~/Views/Shared/"  +  ApplicationConfiguration.CustomerName  + "/{0}.master",
                "~/Views/Shared/{0}.master"
          };
    }
    

    }