title: Routing a subdomain with MVC3
body: <p>I am wanting to create a service for some of my customers that will allow them to have there own webpage on a MVC3 site I have built.</p>
  
  <p>I want a business to be able to have a url that looks like www.BusinessName.mydomain.com</p>
  
  <p>Is it possible to create a route within MVC3 that will allow me to do this?</p>
  
  <p>Thanks for your help</p>
  
  <p>Ok so I think I have gotten a little further.</p>
  
  <p>Ideally I want to be able to have a wildcard for the subdomain in IIS however from the research I have done it looks like this isn't possible.</p>
  
  <p>I have manually added www.mycompany.mydomain.com into IIS and updated my global.ascx to contain this</p>
  
  <pre><code>   public class SubDomainRoute : RouteBase
      {
  
          public override RouteData GetRouteData(HttpContextBase httpContext)
          {
              var url = httpContext.Request.Headers["HOST"];
  
              var subDomain = Helpers.Helpers.GetSubDomain(url);
              if (subDomain==null)
                  return null;
              var routeData = new RouteData(this, new MvcRouteHandler());
  
              switch (subDomain)
              {
                  case "mycompany":
                      routeData.Values.Add("controller", "SubDomain");
                      routeData.Values.Add("action", "Index");
                      break;
                  default:
                      routeData.Values.Add("controller", "Home");
                      routeData.Values.Add("action", "Index");
                      break;
              }
  
              return routeData;
          }
  
          public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>So now I can browse www.mycompanyname.mydomain.com and the correct controller is loaded.</p>
  
  <p>I now have 2 problems.</p>
  
  <p>If I browse to www.mistake.mydomain.com I get a 502 error. What can I do in IIS to redirect to www.mydomain.com if www.mistake.mydomain.com isn't found?</p>
  
  <p>Once I have browsed to www.mycompanyname.mydomain.com all of the links within my main menu are now pointing to e.g. www.mycompanyname.mydomain.com/contactus which is wrong, how do I ensure the menu items are not using the subdomain within the url?</p>