asp.net MVC把Areas区域绑定成二级域名
先分析需求
在MVC项目中,我们如果有两个Areas。比如Test和DEMO。我们的访问地址应该是
http://localhost:8098/test
http://localhost:8098/demo
如果我们绑定域名后想实现这样访问
http://test.abc.com:8098/
http://demo.abc.com:8098/
这个问题的相关解决办法倒是不少,比如http://www.cnblogs.com/jobily/archive/2011/10/09/2204800.html。可惜都是破坏了原来项目的结构,对于已经上线的项目不太适用。
本文的解决办法实在不破坏原来的结构之上,在原来的Areas里面重新绑定新的路由规则,重写基类路由 RouteBase的GetRouteData方法达到绑定二级域名,访问Areas的方法。
前期准备工作你需要先修改本地host文件,绑定一些二级域名。比如
127.0.0.1 demo.abc.com
127.0.0.1 test.abc.com
首先新建 AreaDomainRegistrationContext类。
public class AreaDomainRegistrationContext
{
/// <summary>
///
/// </summary>
/// <param name="_domainName">子域名 如:www.xxx.com 可以传 abc</param>
public AreaDomainRegistrationContext(AreaRegistrationContext _context, string _domainName)
{
domainName = _domainName; context = _context;
}
private string domainName; private AreaRegistrationContext context;
private RouteCollection Routes
{
get
{
if (!DomainRouteTable.DomainRoutes.ContainsKey(domainName))
{
DomainRouteTable.DomainRoutes[domainName] = new RouteCollection();
}
return DomainRouteTable.DomainRoutes[domainName];
}
} public Route MapRoute(string name, string url, object defaults, object constraints = null, string[] namespaces = null)
{
if (namespaces == null && context.Namespaces != null)
{
namespaces = context.Namespaces.ToArray();
} Route route = Routes.MapRoute(name, url, defaults, constraints, namespaces);
route.DataTokens["area"] = context.AreaName;
route.DataTokens["UseNamespaceFallback"] = false;
route.DataTokens["SubDomain"] = domainName;
return route;
}
}
DomainRouteTable类里面有一个静态属性DomainRoutes。主要存储域名和路由之间的存储关联。
public class DomainRouteTable
{
private static Dictionary<string, RouteCollection> _instance = new Dictionary<string, RouteCollection>(); public static Dictionary<string, RouteCollection> DomainRoutes
{
get
{
return _instance;
}
}
}
下面就是重写RouteBase的类了
public class DomainRoute : RouteBase
{
public ResolveDomainName resolveDomainName = DependencyResolver.Current.GetService<ResolveDomainName>();
public RouteData d = new RouteData(null, new StopRoutingHandler()); public override RouteData GetRouteData(HttpContextBase httpContext)
{
string domainName = resolveDomainName.Resolve(httpContext);
//该主机头没有配置,返回 null 继续查找路由
if (domainName == null) return null; if (!DomainRouteTable.DomainRoutes.ContainsKey(domainName)) return d; var rs = DomainRouteTable.DomainRoutes[domainName];
RouteData routeData = rs.GetRouteData(httpContext);
if (routeData == null) return d;
return routeData;
} public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
string domainName = resolveDomainName.Resolve(requestContext.HttpContext); if (domainName == null) return null;
if (!DomainRouteTable.DomainRoutes.ContainsKey(domainName)) return null; var rs = DomainRouteTable.DomainRoutes[domainName];
VirtualPathData vpd = rs.GetVirtualPathForArea(requestContext, values);
return vpd;
}
}
ResolveDomainName主要映射Areas和域名之间的关系,可以根据Areas查找到域名。
public class ResolveDomainName
{
public static Dictionary<string, string> DomainMap = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public string Resolve(HttpContextBase httpContext)
{
string key = httpContext.Request.Headers["host"];
string value;
if (DomainMap.TryGetValue(key, out value)) { return value; }
else return null;
}
}
接下来在Global文件的里面的Application_Start方法里面的第一行加入一下代码
RouteTable.Routes.Add(new DomainRoute());
ResolveDomainName.DomainMap.Add("demo.abc.com:8098", "demo1");
ResolveDomainName.DomainMap.Add("test.abc.com:8098", "Test");
这样就可以绑定多个域名,后面的demo1和Test可以不和areas的名称相等,这里这是一个key而已。
接着在Areas区域里面绑定路由的地方加入一下代码
var context2 = new AreaDomainRegistrationContext(context, "Test");
context2.MapRoute(
name: "Test_default",
url: "{controller}/{action}",
defaults: new { controller = "Home", action = "index" },
namespaces: new string[] { "WebApplication1.Areas.Test.Controllers" });
代码看起来是这样的,这样并不影响之前的访问,还是可以通过areas的方式访问的

同理demo区域也添加代码。
接着编译我们的项目,就可以用demo.abc.com:8098来访问了,是不是很简单。
访问后的效果

本文参考文章地址
https://www.cnblogs.com/hitearth/p/6848788.html
asp.net MVC把Areas区域绑定成二级域名的更多相关文章
- ASP.NET MVC学习之模型绑定(1)
一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...
- 在ASP.NET MVC中使用区域来方便管理controller和view
在ASP.NET MVC中使用区域来方便管理controller和view 在mvc架构中,一般在controllers和views中写所有控制器和视图, 太多控制器时候,为了方便管理,想要将关于pe ...
- Asp.net Mvc 中的模型绑定
asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射. 1.没有模型绑定的时候 public ActionResult Example0() { ) { string id ...
- 000webhost虚拟主机绑定自定义二级域名
作者:荒原之梦 原文链接:http://zhaokaifeng.com/?p=558 前言: 最近想给导航狗IT信息导航做一个文件服务器专门存放文件,以提供引用或下载.于是,我在000webhost上 ...
- ASP.NET MVC使用Areas后怎样获取Area(区域)的名称
写此随笔,目的只为今后在ASP.NET MVC项目中再用到Area(区域)时作为备查. 获取当前Area(区域)名称的方法是: ViewContext.RouteData.DataTokens[&qu ...
- ASP.NET MVC学习之模型绑定(2)
3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了很多过程中的控制,所以我们就需要使用手工的方式进行绑定.下面我们通过一个例子来说明,首先打开 ...
- [转]ASP.NET MVC 4 (九) 模型绑定
本文转自:http://www.cnblogs.com/duanshuiliu/p/3706701.html 模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C ...
- ASP.NET MVC 及 Areas 简单控制路由
ASP.NET MVC中怎么去控制路由,这个想关的文章很多,我在这里就是自我总结一下,仅供参考. 1.我们新建一个项目,查看RouteConfig.cs,代码如下: public static voi ...
- 如何在FineUIMvc(ASP.NET MVC)视图中绑定多个模型?
起因 这是知识星球内的一个网友提出的,按理说ASP.NET MVC中一个视图只能绑定一个模型(Model),在视图顶部标识如下: @model IEnumerable<FineUICore.Ex ...
随机推荐
- SVN概述
----------------------siwuxie095 SVN 概述 1.SVN 即 Subversion 的 ...
- JAVA知识积累 JSP第一篇【JSP介绍、工作原理、生命周期、语法、指令、行为】
什么是JSP JSP全名为Java Server Pages,java服务器页面.JSP是一种基于文本的程序,其特点就是HTML和Java代码共同存在! 为什么需要JSP JSP是为了简化Servle ...
- 给dede添加栏目图片和栏目描述
有的时候我们希望调用栏目时把栏目的图片和描述调出来,但dede好像没有提供栏目图片这个功能,而栏目的描述也是给meta:Description使用的,不是很方便. 所以我们需要自已给dede添加图 ...
- 面向对象设计模式纵横谈:Prototype 原型模式(笔记记录)
有一段时间没写东西了,今天继续把没写完的设计模式写完,今天这堂课是创建型设计模式的最后一堂课,原型设计模式,它同样也是解决了对象在创建的过程中的解耦合的情况,面对变化使代码更稳定,更准确的说是使 ...
- Codeforces 677C. Coloring Trees dp
C. Coloring Trees time limit per test:2 seconds memory limit per test:256 megabytes input:standard i ...
- [SoapUI] 将科学计数法转化为普通数字,并且只保留小数点后几位
方案一: import java.text.NumberFormat class CompareHashMap { def regEx_Numeric = '-?[1-9]\\d*$|-?([1-9] ...
- 如何在c语言中源文件调用另一个源文件的函数
在源文件A1.c中调用A2.c 中的函数有两种方法: 1.在A2.c中有完整的函数定义,在A1.c中添加一下要用到的函数原型(声明)就可以了,例如:在A2.c中:有函数void A2(){...};在 ...
- 13.8.8 div块 居中
<div style="border:1px solid blue;width:760px; height:410px; position:absolute; left:50%; to ...
- TabHost实现通话记录界面
//MainActivity.java public class MainActivity extends TabActivity { @Override public ...
- 用node.js写个在Bash上对字符串进行Base64或URL的encode和decode脚本
一:自己这段时间经常要用到Base64编码和URL编码,写个编译型语言有点麻烦干脆就用node.js弄了个,弄好后在/etc/profile里加上alias就能完成工具的配置,先上代码: functi ...