简体   繁体   English

子域重定向到MVC 3中的根目录

[英]Subdomain redirecting to root in MVC 3

I'm experiencing a frustratingly perplexing issue with regards to subdomains on an MVC 3 application. 关于MVC 3应用程序上的子域,我遇到了一个令人沮丧的困惑问题。 Using the code below, my subdomain routes me back to the root of my domain everytime. 使用下面的代码,我的子域每次都会将我路由回域的根目录。

When I enter subdomain.mydomain.com it routes me to domain.com?nr=0. 当我输入subdomain.mydomain.com时,它会将我路由到domain.com?nr=0。 I can't find why, or where in code, it's appending the querystring value. 我找不到原因,或者在代码中的何处附加了querystring值。

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

            if (!String.IsNullOrEmpty(subDomain) && subDomain != "www" && subDomain != "mydomain")
            {
                var routeData = new RouteData(this, new MvcRouteHandler());
                routeData.Values.Add("controller", "External"); 
                routeData.Values.Add("action", "CoolAction"); 
                routeData.Values.Add("subdomain", subDomain);

                return routeData;
            }
            else
                return null;
        } 
}

I disabled my forms authentication, but that didn't fix it. 我禁用了表单身份验证,但是并没有解决它。 I also have a url rewrite that prepends www. 我也有一个写在www前面的URL重写。 to all requests, if www. 所有要求,如果www。 is missing - I also removed that and it didn't fix it. 丢失了-我也删除了它,但没有解决。 Below is the RegisterRoutes of my Global.asax: 以下是我的Global.asax的RegisterRoutes:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("elmah.axd");

        routes.Add(null, new SubdomainRoute());

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new[] { "MyProject.UI.Web.Controllers" }
        );
        routes.MapRoute("NotesController",
                        "Notes/{noteDestinationType}/{noteDestinationGuid}",
                        new { controller = "Notes", action = "SaveNote" });
    }

I've been banging my head on this all day long and I give up. 我整天都在敲头,我放弃了。 I'm asking for help. 我正在寻求帮助。 TIA. TIA。

I think you are missing regex.Split(url); 我认为您缺少regex.Split(url); See more 查看更多

            var url = httpContext.Request.RawUrl;
            Regex regex = new Regex("/");
            string[] substrings = regex.Split(url);
            var index = host.IndexOf(".");
            var subDomain = host.Substring(0, index);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM