Routing issues in .NET MVC [duplicate]

谁都会走 提交于 2020-01-15 12:36:42

问题


Possible Duplicate:
How to get RouteData by URL?

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);

            if (subDomain != "www" && subDomain != "m")
            {
                var routeData = new RouteData(this, new MvcRouteHandler());
                    routeData.Values.Add("controller", "Business"); 
                    routeData.Values.Add("action", "Display"); 
                    routeData.Values.Add("id", subDomain);
                return routeData;
            }

            if (subDomain == "m")
            {
                var routeData = new RouteData(this, new MvcRouteHandler());
                routeData.Values.Add("controller", "Mobile"); 
                routeData.Values.Add("action", "Index"); 

                return routeData;
            }

            return null;
        }

My problem is that when I access xyz.mydomain.com it is always rerouted to xyz.mydomain.com/Business/Display/xyz. This is preventing me from going to xyz.mydomain.com/Overview as it's picking up the subdomain and redirecting.

I have tried using if statements to determine if a controller is specified, but nothing seems to work. Any suggestions?


回答1:


Just in case anyone else needs an answer this was my solution...
The substrings.Length >= 3 is for a controller then an action so change to 2 if you only need a controller :

public override RouteData GetRouteData(HttpContextBase httpContext)
            {
                var host = httpContext.Request.Headers["HOST"];
                var url = httpContext.Request.RawUrl;
                Regex regex = new Regex("/");
                string[] substrings = regex.Split(url);
                if (substrings.Length >= 3)
                {
                    return null;
                }
                var index = host.IndexOf(".");
                if (index < 0)
                {
                    return null;
                }

                var subDomain = host.Substring(0, index);

                if (subDomain != "www" && subDomain != "m")
                {
                    var routeData = new RouteData(this, new MvcRouteHandler());
                        routeData.Values.Add("controller", "Business"); //Goes to the User1Controller class
                        routeData.Values.Add("action", "Display"); //Goes to the Index action on the User1Controller
                        routeData.Values.Add("id", subDomain);
                    return routeData;
                }

                if (subDomain == "m")
                {
                    var routeData = new RouteData(this, new MvcRouteHandler());
                    routeData.Values.Add("controller", "Mobile"); //Goes to the User2Controller class
                    routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller

                    return routeData;
                }
                return null;
            }


来源:https://stackoverflow.com/questions/10617397/routing-issues-in-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!