Skip Navigation LinksN2 Open Source CMS for ASP.NET > Documentation > Advanced topics > Multiple sites

Multiple sites

Note: The current version also has a troublesome bug when using multiple sites. Please use the intermediate version of N2.dll in the example instead of the release files if you plan to run with multiple sites.

N2 supports using the same database and web application to serve differnt content depending on the visited domain. E.g we could have a content structure that looks like this:

  • Root
    • Main site: http://www.company.com
      • Company information
    • Subsite: http://importantproduct.company.com
      • Product information
    • Other site: http://www.subsidiarycompany.com
      • Other interesting stuff...

Some benefits of this approach compared to having multiple installations:

  • It might be more appealing to users - people who edit content on the site will see everything
  • The sites can run in the same virtual application consuming less memory and resources
  • The sites can share common content resources such as news product information or users
  • Ease of deployment - update all sites at one time

To experiment with multiple sites the current version of N2 (1.3.2) requires some configuration and implementation:

Having some hosts to experiment with

For production you would use your regular DNS service and point out either the same IP address (with host headers) or adding multilpe interfaces to the web server. To test locally you can modify your hosts file located at C:\WINDOWS\system32\drivers\etc\hosts. Add a line for each host:

# Bunch of stuff
# ...
127.0.0.1       localhost
127.0.0.1       alpha.localhost.com #domain name to experiment with
127.0.0.1       beta.localhost.com #domain 2
127.0.0.1       gamma.localhost.com #domain 3

Use IIS

Unfortunatly the Visual Studio built-in web server doesn't work. It doesn't give the application any clues about which domain name the browseris accessing.

Implement ISitesSource

The multiple hosts features requries your root page or 2:nd level pages to implement N2.Web.ISitesSource. The interfaces defines a method that returns information about the web site's start pages and which domain name they should respond to, e.g.:

[N2.Definition("Start Page")]
[N2.Integrity.RestrictParents(typeof(RootPage))]
public class StartPage : PageItem, N2.Web.ISitesSource
{
    [N2.Details.EditableTextBox("Host", 100)]
    public virtual string Host
    {
        get { return (string)(GetDetail("Host") ?? string.Empty); }
        set { SetDetail("Host", value, string.Empty); }
    }

    public IEnumerable<N2.Web.Site> GetSites()
    {
        yield return new N2.Web.Site(Parent.ID, ID, Host);
    }

    public override string IconUrl
    {
        get { return "~/Edit/img/ico/page_world.gif"; }
    }
}

Change web.config

Finally you'll need to configure the N2 engine to enable multiple hosts:

  <castle>
    <properties>...</properties>

     <!-- note: the ordering of these sections is important -->
      <components>
          <component id="n2.sitesProvider" service="N2.Web.ISitesProvider, N2" type="N2.Web.DynamicSitesProvider, N2" />
          <component id="n2.urlParser.multipleSite" service="N2.Web.IUrlParser, N2" type="N2.Web.MultipleHostsUrlParser, N2" />
      </components>
     
    <include uri="assembly://N2/Engine/n2.configuration.xml"/>
  </castle>

Cheat 

Alternatively you could download an example set up like this from here. Good luck and let me know if you experience any problems.

 

powered by N2