【问题标题】:Subdomain redirecting to root in MVC 3子域重定向到 MVC 3 中的根目录
【发布时间】:2013-07-23 08:41:59
【问题描述】:

关于 MVC 3 应用程序上的子域,我遇到了一个令人沮丧的困惑问题。使用下面的代码,我的子域每次都会将我路由回我的域的根目录。

当我输入 subdomain.mydomain.com 时,它会将我路由到 domain.com?nr=0。我找不到为什么或在代码中的何处附加查询字符串值。

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

我禁用了表单身份验证,但这并没有解决问题。我也有一个 URL 重写,前面加上 www。所有请求,如果 www。丢失 - 我也删除了它,但它没有修复它。下面是我的 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" });
    }

我整天都在努力解决这个问题,我放弃了。我在寻求帮助。 TIA。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-3 routing asp.net-mvc-routing


    【解决方案1】:

    我觉得你不见了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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2014-04-10
      • 1970-01-01
      • 2013-09-12
      相关资源
      最近更新 更多