Wildcard Subdomains in MVC5 and Azure WebApp

If you’re here, I’m going to assume you already know what Wildcard Subdomains are.  The point of this post is to teach you how to get them to work in your MVC application, both on your dev machine, and on Azure WebApps.

First step is to get it working on your local machine.  Ben Cull does a great job of getting this working over at his blog: http://benjii.me/2015/02/subdomain-routing-in-asp-net-mvc/  I highly recommend watching his short video on the topic also: https://youtu.be/dMFOo0__rm0

The only problem I ran into at this point was that I would get an exception that the controller wasn’t defined if the URL looked like “subdomain.localhost:4353”.  So, I just added an IF blocks to his SubdomainRoute class to make ‘controller’ “Home” if it is an empty string, and an IF block to make ‘action’ “Index” if it is an empty string.

After this, everything works perfect locally.

To get this to work in Azure WebApps, however; you need to complete a couple more steps.

First, you’ll need to go to your domains DNS management and add the following CNAME record:

*.YourDomain.Com

This gets a Value which looks something like this (Azure will tell you what it needs to be)

YourDomain.azurewebsites.net

Next, in Azure, go to your WebApp and click “Configure” and then scroll down and click “Manage Domains”.  Here, you’ll add the following domain name:

*.YourDomain.com

Note: This is where Azure will tell you what to put in your CNAME record.

You’re all set.  Now you can go to “AnySubdomainThatYouWant.YourDomain.Com” and it should work!

 

Advertisements