Subdomain Routing w/ MVC not working

Discussion in 'General troubleshooting' started by matticus1181, Apr 9, 2010.

  1. Hi, I have a MVC application that I'm attempting to use subdomain routing with. It's working fine on my local box where I have my subdomains (subname.localhost) routed to 127.0.0.1 in my hosts file.

    Unfortunately, when I enter my subdomain url on the live site, I get a 404...

    Here is my global.asax.cs file:

    Code:
    public class MvcApplication : System.Web.HttpApplication {
            public static void RegisterRoutes(RouteCollection routes) {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.Add(new SubdomainRoute());
    
                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new {
                        controller = "Home", action = "Index", id = UrlParameter.Optional
                    } // Parameter defaults
                );            
    
            }
    
            protected void Application_Start() {
                AreaRegistration.RegisterAllAreas();
    
                RegisterRoutes(RouteTable.Routes);
            }
        }
    
        public class SubdomainRoute : RouteBase {
    
            public override RouteData GetRouteData(HttpContextBase httpContext) {
                var url = httpContext.Request.Headers["HOST"];
                var index = url.IndexOf(".");
    
                if (index < 0)
                    return null;
    
                var subDomain = url.Substring(0, index).ToLower();
    
                var routeData = new RouteData(this, new MvcRouteHandler());
    
                switch (subDomain) {
                    case "formula1":
                        routeData.Values.Add("controller", "Formula1"); //Goes to the Formula1Controller class
                        routeData.Values.Add("action", "Index"); //Goes to the Index action on the Formula1Controller
                        break;
                    case "le-mans":
                        routeData.Values.Add("controller", "LeMans"); //Goes to the LeMansController class
                        routeData.Values.Add("action", "Index"); //Goes to the Index action on the LeMansController
                        break;
                    case "vintage-racing":
                        routeData.Values.Add("controller", "Vintage_Racing"); //Goes to the Vintage_RacingController class
                        routeData.Values.Add("action", "Index"); //Goes to the Index action on the Vintage_RacingController
                        break;
                    case "mustang-restoration":
                        routeData.Values.Add("controller", "Mustang_Restoration"); //Goes to the Mustang_RestorationController class
                        routeData.Values.Add("action", "Index"); //Goes to the Index action on the Mustang_RestorationController
                        break;
                    case "mustangrestoration":
                        routeData.Values.Add("controller", "Mustang_Restoration"); //Goes to the Mustang_RestorationController class
                        routeData.Values.Add("action", "Index"); //Goes to the Index action on the Mustang_RestorationController
                        break;
                    default:
                        routeData.Values.Add("controller", "Home"); //Goes to the Mustang_RestorationController class
                        routeData.Values.Add("action", "Index"); //Goes to the Index action on the Mustang_RestorationController
                        break;
                }
    
                return routeData;
            }
    
            public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
                //Implement your formating Url formating here
                return null;
            }
        }
    I have my controllers/views set up in the same directory structure that the default "Home" route one is set up as. The default "Home" routing does work.

    Also, I have my GoDaddy account set up to "forward with masking" formula1.globalmotorsporting.com to www.globalmotorsporting.com, though I've tried it without the forwarding as well and it still didn't work...

    Anyone have any ideas why my subdomains aren't working? Thanks!
     
  2. Ok, I'm an idiot. If you want to use a subdomain, you have to use the subdomain manager and actually get the DNS updated to point to your main domain, duh.
     
  3. No worries. Glad you got it sorted out.
     

Share This Page