是否可以使用ASP.NET MVC路由使用子域信息来确定其路由?例如:
user1 .domain.com去了一个地方
user2 .domain.com转到另一个?
或者,我可以这样做,所以这两个都与username
参数进入相同的控制器/动作?
您可以通过创建新路由并将其添加到global.asax中RegisterRoutes中的routes集合来实现.以下是自定义Route的一个非常简单的示例:
public class ExampleRoute : 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 (subDomain == "user1") { var routeData = new RouteData(this, new MvcRouteHandler()); routeData.Values.Add("controller", "User1"); //Goes to the User1Controller class routeData.Values.Add("action", "Index"); //Goes to the Index action on the User1Controller return routeData; } if (subDomain == "user2") { var routeData = new RouteData(this, new MvcRouteHandler()); routeData.Values.Add("controller", "User2"); //Goes to the User2Controller class routeData.Values.Add("action", "Index"); //Goes to the Index action on the User2Controller return routeData; } return null; } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { //Implement your formating Url formating here return null; } }
要在保留标准MVC5路由功能的同时捕获子域,请使用以下SubdomainRoute
派生的类Route
.
此外,SubdomainRoute
允许子域可选地指定为查询参数,制作sub.example.com/foo/bar
和example.com/foo/bar?subdomain=sub
等效.这允许您在配置DNS子域之前进行测试.查询参数(在使用时)通过由Url.Action
等生成的新链接传播.
query参数还允许使用Visual Studio 2013进行本地调试,而无需使用netsh进行配置或以管理员身份运行.默认情况下,IIS Express仅在未提升时绑定到localhost ; 它不会绑定到sub.localtest.me这样的同义主机名.
class SubdomainRoute : Route { public SubdomainRoute(string url) : base(url, new MvcRouteHandler()) {} public override RouteData GetRouteData(HttpContextBase httpContext) { var routeData = base.GetRouteData(httpContext); if (routeData == null) return null; // Only look at the subdomain if this route matches in the first place. string subdomain = httpContext.Request.Params["subdomain"]; // A subdomain specified as a query parameter takes precedence over the hostname. if (subdomain == null) { string host = httpContext.Request.Headers["Host"]; int index = host.IndexOf('.'); if (index >= 0) subdomain = host.Substring(0, index); } if (subdomain != null) routeData.Values["subdomain"] = subdomain; return routeData; } public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { object subdomainParam = requestContext.HttpContext.Request.Params["subdomain"]; if (subdomainParam != null) values["subdomain"] = subdomainParam; return base.GetVirtualPath(requestContext, values); } }
为方便起见,请按照MapSubdomainRoute
您的RegisterRoutes
方法调用以下方法MapRoute
:
static void MapSubdomainRoute(this RouteCollection routes, string name, string url, object defaults = null, object constraints = null) { routes.Add(name, new SubdomainRoute(url) { Defaults = new RouteValueDictionary(defaults), Constraints = new RouteValueDictionary(constraints), DataTokens = new RouteValueDictionary() }); }
最后,为了方便地访问子域(来自真正的子域或查询参数),使用此Subdomain
属性创建Controller基类是有帮助的:
protected string Subdomain { get { return (string)Request.RequestContext.RouteData.Values["subdomain"]; } }
这不是我的工作,但我必须在这个答案上添加它.
这是一个很好的解决方案.Maartin Balliauw编写的代码创建了一个DomainRoute类,可以与普通路由非常相似地使用.
http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx
样品使用就像这样......
routes.Add("DomainRoute", new DomainRoute( "{customer}.example.com", // Domain with parameters "{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } // Parameter defaults ))
;