是否有可能使基于子域的ASP.NET MVC路线? [英] Is it possible to make an ASP.NET MVC route based on a subdomain?
本文介绍了是否有可能使基于子域的ASP.NET MVC路线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能有一个使用子域信息来确定其路由的ASP.NET MVC路线?例如:
Is it possible to have an ASP.NET MVC route that uses subdomain information to determine its route? For example:
- 用户1 .domain.com 的去一个地方
- 用户2 .domain.com 的去到另一个?
- user1.domain.com goes to one place
- user2.domain.com goes to another?
或者,我可以把它使这两个去同一个控制器/动作与用户名
参数?
Or, can I make it so both of these go to the same controller/action with a username
parameter?
推荐答案
您可以通过创建一个新的途径,并在您的Global.asax它添加到路由集合中的RegisterRoutes做到这一点。下面是一个自定义路线的一个很简单的例子:
You can do it by creating a new route and adding it to the routes collection in RegisterRoutes in your global.asax. Below is a very simple example of a custom 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;
}
}
这篇关于是否有可能使基于子域的ASP.NET MVC路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文