啊哈!,这花了一些时间(以及旧书的巅峰),但我想我已经解决了。
您需要创建自定义路由约束。
这是我快速制作的:
public class hostnameConstraint : IRouteConstraint
{
protected string _hostname;
public hostnameConstraint (string hostname)
{
_hostname = hostname;
}
bool IRouteConstraint.Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (httpContext.Request.Url.Host == _hostname)
return true;
return false;
}
}
然后您只需将其添加到您的路由并指定您希望路由应用到哪个主机名。像这样:
routes.MapRoute(
"ImageGallery", "{controller}/{action}",
new { controller = "Home", action = "Index"},
new { hostname = new hostnameConstraint("webhost1.com") }
);
routes.MapRoute(
"ImageGallery", "{controller}/{action}",
new { controller = "Home", action = "Index"},
new { hostname = new hostnameConstraint("webhost2.com") }
);
等等等等。我不知道您的路线是如何布置的,但关键是现在您可以为主机名设置单独的路线。这应该能让你做你想做的事。