可能重复:
如何通过 URL 获取 RouteData?
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;
}
我的问题是,当我访问xyz.mydomain.com
它时,它总是被重新路由到xyz.mydomain.com/Business/Display/xyz
. 这阻止了我去,xyz.mydomain.com/Overview
因为它正在拾取子域并重定向。
我尝试使用if
语句来确定是否指定了控制器,但似乎没有任何效果。有什么建议么?