• Subdomain.Plugin

Subdomain.Plugin

Assembly: Subdomain.Plugin

Description

A subdomain allows you to seperate portions of a website or API into a dedicated heirerarchy, for instance you could have specific subdomains for:

  • Searching
  • Login
  • Blogs
  • Helpdesk
  • Specific API Versions

The default subdomain for any website is typically www (world wide web) and most websites will easily function with a single subdomain, using MVC and a domain name of mywebsite.com, the default uri would be

www.mywebsite.com/

The above uri would invoke the home controller and the same page as a uri of www.mywebsite.com/home (assuming a standard setup). Given the above examples we would have controllers for specific areas e.g.

www.mywebsite.com/Login
www.mywebsite.com/Blogs
www.mywebsite.com/Helpdesk
www.mywebsite.com/api/v2/

To reconfigure to use subdomains for the above you would instead have something similar to:

login.mywebsite.com/
blogs.mywebsite.com/
helpdesk.mywebsite.com/
apiv2.mywebsite.com/

This allows us to split the behaviour of a website into individual subdomains with specific areas of responsibility.

Subdomain Attribute

To enable subdomain routing for an attribute you must add a SubdomainAttribute to the controller:

[Subdomain("Blog")]
public class BlogController : Controller

The name of the subdomain attribute must correspond to an entry in the Configuration section in order to enable the subdomain routing.

Configuration

Configuring the subdomain plugin is accomplished using appsettings.json. By default all subdomain routing is disabled, each subdomain has to be specifically enabled in order for it to work. See DNS topic for configuring CNAME or A records.

"SubdomainSettings": {
  "DomainName""mywebsite.com",
  "Enabled"true,
  "Subdomains": {
    "Blog": {
      "Disabled"false,
      "RedirectedRoute""blog",
      "PermanentRedirect"true
    },
    "Helpdesk": {
      "Disabled"false,
      "RedirectedRoute""helpdesk",
      "PermanentRedirect"true
    },
    "Account": {
      "Disabled"false,
      "RedirectedRoute""account",
      "PermanentRedirect"true
    }
  }

There are three distinct options to configure for enabling subdomain routing, they are:

  • DomainName - This is the name of the primary domain
  • Enabled - Setting to true enables subdomain routing, false disables this feature completely.
  • Subdomains - Configuration settings for each subdomain

Each Subdomain settings has a further 3 options:

  • Disabled - Setting to false enables subdomain routing, true disables this feature completely.
  • RedirectedRoute - This is the name of subdomain under the primary domain
  • PermanentRedirect - Determines whether the response for redirecting to a subdomain is permanent or not.

It is important to note that the name of the subdomain within the subdomains list has to correspond with the subdomain attribute applied to each controller.

DNS

It is important to note that CNAME or A records will need to be created for each type of subdomain within a root domain. This is typically accomplished by configuring the DNS entries for each domain using your domain host.

Drawbacks

Although relatively easy to setup, additional steps need to be considered when using subdomains, they include:

  • If cookies are to be read across all subdomains, you need to ensure they have the domain set to the primary domain, i.e. ".mysebsite.com"
  • If using SSL you will need to ensure you have individual SSL certificates for each subdomain or a wildcard certificate.
  • Consideration for same-origin policy must be made.
  • Ensure security implication are met especially in respect ot cross site scripting.

Middleware

The Subdomain middleware class can be used in any supported MVC website by manually configuring the application builder

app.UseSubdomainRouting();

If used as part of the plugin manager configuration is automatic.