Hosting Two Different Domains in Same ASP.NET MVC Site
Motivation – I am Cheap!
I have a hosting account at DiscountASP.NET that I have used as a playground for anything I want to host on the web. My personal Coding Out Loud site is hosted there (with a few directories that are not linked, for example). Since starting the Boston Azure User Group, I need a place to host a page. While I would eventually like to host the user group site on Azure itself, for now I have produced a simple ASP.NET MVC site. To avoid paying for two hosting accounts, I am reusing my existing account – sharing the hosting across bostonazureusergroup.org and codingoutloud.com – but still maintaining their distinct identities.
The approach I take makes sense to me since I want a solution that is:
- Very simple
- Easy to manage once implemented
- Treats bostonazureusergroup.org as the main site, but “tolerates” codingoutloud.com being around (in other words, maintainability/risk of errors for codingoutloud.com is less important)
In order to host two sites with a single hosting account, you have a few steps:
- Add a Domain Pointer with your ISP. If, like me, your site is hosted on a shared IP address, this step is necessary so that the site’s IIS web server knows which top-level directory your domain is associated with. In my case, this tells both codingoutloud.com and bostonazureusergroup.org to go to the same site.
- Configure your DNS. This is easier than it sounds. Your ISP will tell which DNS servers your should point to. You could also deal with Godaddy’s Domain Forwarding and cloaking, but for a one-time cost of $15 with my ISP (DiscountASP.NET) I just added the pointer – a cleaner solution, and possible more search-engine-friendly.
- Note that the following changes to the code are only necessary if you want different results dependent on which domain is used to access the site – in other words, I don’t want my bostonazureusergroup.org visitors to be stumbling across any codingoutloud.com artifacts, and vice versa. The following screen shot includes the hack simple code modifications – they are circled – a private method with a little dirty work, invoke the method in the right place, and display a specific View. Note that the view is specified in a different file – and under a folder that is specific to your secondary site – CodingOutLoud in my case.
To create the …/Views/CodingOutLoud/Index.aspx view shown in the right-hand pane in the screen shown above, you would do the following:
- Right-Click on Views folder and choose Add > New Folder from the pop-up. I called mine “CodingOutLoud” and it is shown above.
- Right-Click on the newly created Views/CodingOutLoud folder, this time choose Add > View… from the pop-up, as shown below. I called my View “Index” (and since it lives under CodingOutLoud folder it does not clash with other Views with that name, given the default routing rules).
Name your view – and I also chose not to use the master page, so I unchecked it:
Once created, …/Views/CodingOutLoud/Index.aspx can contain any code you like – even plain old HTML.
This is all it took to get this simple approach to work. Now when users visit via “codingoutloud.com” they are diverted to …/Views/CodingOutLoud/Index.aspx, and otherwise the usual machinery of ASP.NET MVC takes over.
Other Potential Approaches
I considered, but did not adopt, some other approaches – mostly since I wanted to do something very simple. Here are some of the other approaches:
- ASP.NET MVC Subdomain Routing
- ASP.NET MVC Domain Routing
- Subdomains for a single application with ASP.NET MVC
- ASP.NET MVC root url’s with generic routing