Julian Dormon
Julian Dormon

Reputation: 1779

How can I host multiple websites in IIS 7 and use the same MVC application for all them?

How can I host multiple websites in IIS 7 and use the same MVC application for all them such that each website:

  1. Has it's own root directory so user's can upload their own files (html, css, javascript) without seeing other websites'content

  2. Uses the same MVC application - so updates to the application affect all sites

  3. Users cannot affect the MVC application in any way (preferably it will be hidden from them)

Thanks!

Upvotes: 1

Views: 5833

Answers (2)

viperguynaz
viperguynaz

Reputation: 12174

If you are running one code base, then setup one site that uses domain routing or a global filter to capture the domain, save it and use it as a key for folders, etc...

public class DomainFilterAttribute : ActionFilterAttribute
{
    private SiteDomainRepository _siteDomainRepository = new SiteDomainRepository();

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var viewBag = filterContext.Controller.ViewBag;
        var host = HttpContext.Current.Request.IsLocal ? "www.testdomain.com" : HttpContext.Current.Request.Url.Host;
        var domain = hostSplit[1];

        var siteDomain = _siteDomainRepository.GetByRowKey(domain);

        viewBag.DomainName = host.ToLower();
        viewBag.Domain = siteDomain.RowKey;
        viewBag.Class = siteDomain.Key;
        viewBag.TrackingCode = siteDomain.TrackingCode;
    }
}

Upvotes: 0

Dai
Dai

Reputation: 155270

  1. Create a directory where you deploy the MVC files, named "AppFoo". Note that the configuration file web.config will be shared between all of the sites, you'll have to load per-site configuration at on-demand runtime by looking at the Host HTTP header or other binding information.
  2. Create each website in IIS and get it to use the the application directory you created. Ideally each website runs under its own application pool with its own user-account, all of the user-accounts will have access to AppFoo directory.
  3. Create a new directory somewhere, e.g. under C:\Inetpub\sites\ named WebsiteA, WebsiteB, etc. Each folder re-sets the NTFS permission ACLs that only allows each website's application pool user access. Inside these create folders called "Content", "Views" and "Logs"
  4. For each website in IIS (I assume you're using the root) set up a Virtual Directory called "Content" and "Views" (which is how I assume you have set-up your MVC applications), map these to subdirectories of the folders you created in Step 3.
  5. For each website, go to the Logging page and set the log-path to the Logs directory you created for the site in step 3.

Note that this plan is based on the assumption that MVC evaluates IIS virtual directories in its VirtualPathProvider. If not then you'll have to roll your own VirtualPathProvider to load per-website content, again, on a per-hostname basis.

Upvotes: 1

Related Questions