Asp.net mvc 5 MVC5中的域路由

Asp.net mvc 5 MVC5中的域路由,asp.net-mvc-5,Asp.net Mvc 5,我意识到有一些关于类似事情的文章,但没有一篇对我有帮助 我有一个web应用程序,其中多个域指向同一个应用程序。我想知道如何根据尝试访问web应用的域将路由映射到特定控制器 我目前尝试的是以下内容,但没有成功 routes.MapRoute( name: "WowPoetry", url: "wowpoetry.org/{action}/{id}", defaults: new { controller = "Wow", action = "Index", id = Url

我意识到有一些关于类似事情的文章,但没有一篇对我有帮助

我有一个web应用程序,其中多个域指向同一个应用程序。我想知道如何根据尝试访问web应用的域将路由映射到特定控制器

我目前尝试的是以下内容,但没有成功

routes.MapRoute(
    name: "WowPoetry",
    url: "wowpoetry.org/{action}/{id}",
    defaults: new { controller = "Wow", action = "Index", id = UrlParameter.Optional }
);

我最终完成了本教程,以实现我想要的,除了我曾经做域路由,而不是子域路由

实施:

添加(新域路由(“wowpoetry.org”,new{controller=“Wow”,action=“Index”}); DomainData.cs

公共类域数据
{
公共字符串协议{get;set;}
公共字符串主机名{get;set;}
公共字符串片段{get;set;}
}
DomainRoute.cs

公共类域路由:路由
{
私有Regex域Regex;
私有正则表达式pathRegex;
公共字符串域{get;set;}
公共域路由(字符串域、字符串url、RouteValueDictionary默认值)
:base(url,默认值,新的MvcRouteHandler())
{
域=域;
}
公共域路由(字符串域、字符串url、RouteValueDictionary默认值、IRoutHandler routeHandler)
:base(url、默认值、routeHandler)
{
域=域;
}
公共域路由(字符串域、字符串url、对象默认值)
:base(url、新RouteValueDictionary(默认值)、新MvcRouteHandler())
{
域=域;
}
公共域路由(字符串域、字符串url、对象默认值、IRouteHandler routeHandler)
:base(url、新RouteValueDictionary(默认值)、routeHandler)
{
域=域;
}
公共覆盖路由数据GetRouteData(HttpContextBase httpContext)
{
//构建正则表达式
domainRegex=CreateRegex(域);
pathRegex=CreateRegex(Url);
//请求信息
string requestDomain=httpContext.Request.Headers[“主机”];
如果(!string.IsNullOrEmpty(requestDomain))
{
if(requestDomain.IndexOf(“:”)大于0)
{
requestDomain=requestDomain.Substring(0,requestDomain.IndexOf(“:”);
}
}
其他的
{
requestDomain=httpContext.Request.Url.Host;
}
string requestPath=httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2)+
httpContext.Request.PathInfo;
//匹配域和路由
Match domainMatch=domainRegex.Match(requestDomain);
Match-pathMatch=pathRegex.Match(请求路径);
//路由数据
RoutedData=null;
if(domainMatch.Success&&pathMatch.Success&&requestDomain.ToLower()!=“tg.local”&&
requestDomain.ToLower()!=“tg.terrasynq.net”和&requestDomain.ToLower()!=“www.townsgossip.com”&&
requestDomain.ToLower()!=“townsgossip.com”)
{
数据=新的路由数据(这是RouteHandler);
//首先添加默认值
if(默认值!=null)
{
foreach(默认值中的KeyValuePair项)
{
data.Values[item.Key]=item.Value;
}
}
//迭代匹配域组
对于(int i=1;i