对于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。
答案 0 :(得分:0)
我认为你遗失了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);