ASP.NET MVC 实现二级域名
自从微软发布 ASP.NET MVC 和routing engine (System.Web.Routing)以来,就设法让我们明白你完全能控制URL和routing,只要与你的application path相结合进行扩展,任何问题都迎刃而解。如果你需要在所处的域或者子域处理数据标记的话,强制使用Default。
遗憾的是,ASP.NET MVC是基于虚拟目录的,在实际项目却有各种各样的需求方案。
例如:
1:应用程序是多语言的,像cn.example.com应该被匹配到“www.{language}example.com”路由上。
2:应用程序是多用户的,像username.example.com应该被匹配到“www.{clientname}.example.com”路由上。
3:应用程序是多子域的,像mobile.example.com应该被匹配到"www.{controller}.example.com/{action}....” 。
坐下来,深呼吸,开始我们ASP.NET MVC的神奇之旅吧。
定义routes
下面是我们定义简单的route,不带任何controller控制的route:
另一个例子是用我们的controller控制域名:
打算用controller 和action完全控制域名?
接下来是多语言route:
HtmlHelper 扩展方法
因为我们不希望所有的URL所产生HtmlHelper ActionLink要使用full URLs,第一件事我们会添加一些新的ActionLink,其中载有boolean flag是否要full URLs或没有。利用这些,现在您可以添加一个链接到一个Action如下:
跟你以往的习惯没有什么不同,不是吗? 以下是一小段代码:
// more of thesepublicstaticstring ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes, bool requireAbsoluteUrl) { if (requireAbsoluteUrl) { HttpContextBase currentContext =new HttpContextWrapper(HttpContext.Current); RouteData routeData = RouteTable.Routes.GetRouteData(currentContext);
routeData.Values["controller"] = controllerName; routeData.Values["action"] = actionName;
DomainRoute domainRoute = routeData.Route as DomainRoute; if (domainRoute !=null) { DomainData domainData = domainRoute.GetDomainData(new RequestContext(currentContext, routeData), routeData.Values); return htmlHelper.ActionLink(linkText, actionName, controllerName, domainData.Protocol, domainData.HostName, domainData.Fragment, routeData.Values, null); } } return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes); } }
在这没什么特别的:有许多的扩展方法,把扩展的URL加到域名上。这是一个预设ActionLink helpers,我的精神食粮来了DomainRoute class(详见:Dark Magic)
Dark magic
瞥眼之间,您可能已经看到了我的DomainRoute类代码段。这个类实际上是提取子域,并增加了象征性支持域部分的传入的URL,
我们将扩展基类,它已经给了我们一些属性和方法,但是我们得重写他们!
// publicoverride RouteData GetRouteData(HttpContextBase httpContext) { // 构造regex domainRegex = CreateRegex(Domain); pathRegex = CreateRegex(Url);
// 请求信息string requestDomain = httpContext.Request.Headers["host"]; if (!string.IsNullOrEmpty(requestDomain)) { if (requestDomain.IndexOf(":") >0) { requestDomain = requestDomain.Substring(0, requestDomain.IndexOf(":")); } } else { requestDomain = httpContext.Request.Url.Host; } string requestPath = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
//匹配域名和路由 Match domainMatch = domainRegex.Match(requestDomain); Match pathMatch = pathRegex.Match(requestPath);
// Route 数据 RouteData data =null; if (domainMatch.Success && pathMatch.Success) { data =new RouteData(this, RouteHandler);
// 添加默认选项if (Defaults !=null) { foreach (KeyValuePair<string, object> item in Defaults) { data.Values[item.Key] = item.Value; } }
// 匹配域名路由for (int i =1; i < domainMatch.Groups.Count; i++) { Group group = domainMatch.Groups[i]; if (group.Success) { string key = domainRegex.GroupNameFromNumber(i); if (!string.IsNullOrEmpty(key) &&!char.IsNumber(key, 0)) { if (!string.IsNullOrEmpty(group.Value)) { data.Values[key] = group.Value; } } } }
// 匹配域名路径for (int i =1; i < pathMatch.Groups.Count; i++) { Group group = pathMatch.Groups[i]; if (group.Success) { string key = pathRegex.GroupNameFromNumber(i); if (!string.IsNullOrEmpty(key) &&!char.IsNumber(key, 0)) { if (!string.IsNullOrEmpty(group.Value)) { data.Values[key] = group.Value; } } } } }
return data; }
publicoverride VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) { returnbase.GetVirtualPath(requestContext, RemoveDomainTokens(values)); }
public DomainData GetDomainData(RequestContext requestContext, RouteValueDictionary values) { // 获得主机名string hostname = Domain; foreach (KeyValuePair<string, object> pair in values) { hostname = hostname.Replace("{"+ pair.Key +"}", pair.Value.ToString()); }
// Return 域名数据returnnew DomainData { Protocol ="http", HostName = hostname, Fragment ="" }; }
// }
哇,这是一串按照我们定义的route转换传入请求的URL到tokens的代码,我们这样做是转换{controller}和按照regex然后再尝试匹配route规则,在我们的DomainRoute class里还有其他的helper方法,需要更多的功能可以自己研究扩展。
附代码:附件 (如果要在使用Visual Studio开发Web服务器,务必添加把二级域名添加到hosts文件)(貌似本地测试不用) 原文地址:http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx 其实有的人为什么要这么麻烦用这种方式,URL重写或者二级域名直接绑定都可以。但是既然微软给url rouring,就应该能做到。
转自:http://www.cnblogs.com/luanwey/archive/2009/08/12/1544444.html
转载于:https://www.cnblogs.com/cuihongyu3503319/p/3408400.html
ASP.NET MVC 实现二级域名相关推荐
- ASP.NET MVC 实现二级域名(泛域名)
自从微软发布 ASP.NET MVC 和routing engine (System.Web.Routing)以来,就设法让我们明白你完全能控制URL和routing,只要与你的application ...
- ASP.NET MVC动态二级域名及DNS(泛解析配置)
动态二级域名的实现: 应用场景:目前产品要实现SaaS功能,因为工作需要实现二级域名:www.{CompanyUrl}.xxx.com 假设产品主域名入口为:www.xxx.com 当a公司租户登录时 ...
- MVC利用Routing实现多域名绑定一个站点、二级域名以及二级域名注册Area
最近有这么个需求:在一个站点上绑定多个域名,每个域名进去后都要进入不同的页面.实现了这个功能以后,对于有多个域名,且有虚拟空间,但是虚拟空间却只匹配有一个站点的用户来说,可以节省很多小钱钱. 很久以前 ...
- asp.net core mvc中如何把二级域名绑定到特定的控制器上
由于公司的工作安排,一直在研究其他技术,所以一直没时间更新博客,今天终于可以停下手头的事情,写一些新内容了. 应用场景:企业门户网站会根据内容不同,设置不同的板块,如新浪有体育,娱乐频道,等等.有的情 ...
- 二级域名用asp.net 2.0的实现方案
本人所了解有两种方案,可能还有其的方式,希望大家多多讨论! 基本思路: 1. 域名支持泛解析,即是指:把A记录 *.域名.com 解析到服务器IP,服务器IIS中做绑定,绑定时主机头为空; 2. 为 ...
- asp.net用url重写URLReWriter实现任意二级域名 (转)
asp.net用url重写URLReWriter实现任意二级域名 Asp.net 用url重写(URLReWriter)实现任意二级域名 好久没有写技术文章,如果大家看不明白,就多看几篇,汗,或者,在 ...
- asp.net 二级域名(路由方式实现)
自从微软发布 ASP.NET MVC 和routing engine (System.Web.Routing)以来,就设法让我们明白你完全能控制URL和routing,只要与你的application ...
- ASP.NET“.NET研究”下用URLRewriter重写二级域名
这里要求对域名进行重写,实现http://1234.abc.com/ 到 ~/Defa.aspx?id=1234的重写. 第一:域名 首先域名要支持泛解悉,就是域上海企业网站制作名解悉的主机名为星号* ...
- 用asp.net 2.0实现网站二级域名(转)
以下是以www.域名.com这个域名为例,让每个注册用户都有自已的二级域名,其中abc.域名.com就是要用到的二级域名 基本思路: 1. 域名支持泛解析,即是指:把A记录 *.域名.com 解析到 ...
最新文章
- 妙用 background 实现花式文字效果
- Python基础语法-Python,Java,C++变量互换值的区别
- 洛谷 P1417 烹调方案 (01背包拓展)
- c++面向对象高级编程 学习十四 引用
- linux mysql主从配置_Linux下Mysql主从同步配置
- 扎克伯格靠AI挺过危机,Facebook满血复活还需3年
- Atitit 知识图谱解决方案:提供完整知识体系架构的搜索与知识结果overview
- python scipy.stats 正态分布_Python Scipy stats.normaltest()用法及代码示例
- 图像增广——图片旋转任意角度(python实现)
- 机器学习-UCI数据集
- 校园网组网方案的设计
- java udp转发_【Java】UDP发包的简单实现
- IPCAS1.2.11安装步骤
- office2010卸载不掉解决办法
- Flink Table和SQL中Table和DataStream的相互转换(fromDataStream、toChangelogStream、attachAsDataStream)
- 首个“中国籍”曲妥珠单抗于欧盟获批上市
- 自建ss报错500 Internal Privoxy Error
- 架构(B站尚硅谷大数据项目实践 电影推荐系统概述)
- 计算自然数e以及怎样理解为什么出现这么一个数
- Spring获取应用上下文通用类SpringContextHolder
热门文章
- 再见BOBO,从此梅阿查再无国王!
- 【Python-ML】SKlearn库线性回归器LinearRegression
- GCN代码超详解析Two-stream adaptive graph convolutional network for Skeleton-Based Action Recognition(一)
- Administer Service Cloud
- 微服务架构编码,构建
- C# UI界面的更新
- Python 库兼容性问题-fromstring() has been removed. Please call frombytes() instead.原因及解决办法
- 模拟电路技术之基础知识(四)
- CTFshow 文件包含 web117
- OpenCV学习--saturate_cast防止数据溢出