ASP.NET MVC 路由进阶(之二)--自定义路由约束
3.自定义路由约束
什么叫自定义路由约束呢?假如路由格式为archive/{year}/{month}/{day},其中year,month,day是有约束条件的,必须是数字,而且有一定范围。
这时候,我们就可以设置约束类,进行自定义路由约束了。
第一步: 我们先添加年份限制类 YearRouteConstraint。请看代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Globalization; namespace MvcMobileDMS.App_Start
{
public class YearRouteConstraint:IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.IncomingRequest && parameterName.ToLower(CultureInfo.InvariantCulture) == "year")
{
try
{
int year = Convert.ToInt32(values["year"]);
if ((year >= ) && (year <= )) return true;
}
catch
{
return false;
}
}
return false;
}
}
}
第二步:添加月份限制类 MonthRouteConstraint。请看代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Globalization; namespace MvcMobileDMS.App_Start
{
public class MonthRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.IncomingRequest && parameterName.ToLower(CultureInfo.InvariantCulture) == "month")
{
try
{
int month = Convert.ToInt32(values["month"]);
if ((month >= ) && (month <= )) return true;
}
catch
{
return false;
}
}
return false;
}
}
}
第三步:添加日期限制类DayRouteConstraint。请看代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Globalization; namespace MvcMobileDMS.App_Start
{
public class DayRouteConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (routeDirection == RouteDirection.IncomingRequest && parameterName.ToLower(CultureInfo.InvariantCulture) == "day")
{
try
{
int month = Convert.ToInt32(values["month"]);
int day = Convert.ToInt32(values["day"]);
if (day < ) return false;
switch (month)
{
case :
case :
case :
case :
case :
case :
case :
if (day <= ) return true;
break;
case :
if (day <= ) return true;
break;
case :
case :
case :
case :
if (day <= ) return true;
break;
}
}
catch
{
return false;
}
}
return false;
}
}
}
ok,三个限制类已经写完了,需要在全局应用程序类中配置路由,见代码清单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing; namespace MvcMobileDMS
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DMS", action = "logon", id = UrlParameter.Optional }
); routes.MapRoute(
"Archive",
"archive/{year}/{month}/{day}",
new { controller = "Archive", action = "Index", year = "", month = "", day = "" }, new { year = new MvcMobileDMS.App_Start.YearRouteConstraint(), month = new MvcMobileDMS.App_Start.MonthRouteConstraint(), day =new MvcMobileDMS.App_Start.DayRouteConstraint() }
); }
}
}
我们看看运行结果:
当我在浏览器地址栏输入以下地址时,http://localhost:7449/archive/1988/9/10,如下截图:

而当我输入不满足约束条件的地址时,http://localhost:7449/archive/1988/09/34,如下截图:

由此可见,当输入非法路由时,路由系统会当做错误处理,所以我们可以为MVC路由设置约束,以规范路由格式。
好了,今天写到这里。希望能对你有所帮助O(∩_∩)O哈哈~
ASP.NET MVC 路由进阶(之二)--自定义路由约束的更多相关文章
- ASP.NET MVC简单编程之(二)经典路由篇
话题:请求从路由开始 在实际的ASP.NET MVC开发中,URL访问规则----路由的定义是非常重要的.因为任何一个请求都离不开路由.理解它,我们将能理解MVC处理请求的整个过程,灵活地定义系统各种 ...
- Asp.net mvc 知多少(二)
本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...
- ASP.NET MVC案例教程(二)
ASP.NET MVC案例教程(二) 让第一个页面跑起来 现在,我们来实现公告系统中的第一个页面——首页.它非常简单,只包括所有公告分类的列表,并且每个列表项是一个超链接.其中分类数据是用我们的Moc ...
- MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)
前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...
- ASP.NET MVC学习笔记(二)笔记
接下来我们一起了解ASP.NET MVC的最重要的核心技术,了解ASP.NET MVC的开发框架,生命周期,技术细节. 一.Routing与ASP.NET MVC生命周期 1.Routing——网址路 ...
- ASP.NET MVC 5 入门-2控制器、路由
一.创建项目: 上起始页,选择新项目. 在中新的项目对话框中,右侧语言类别选择C# ,然后项目类型选择Web,然后选择ASP.NET Web 应用程序 (.NET Framework) 项目模板. 将 ...
- asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证
原文:asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证 在前面的文章中我们曾经涉及到ControllerActionInvoker类GetPara ...
- Asp.Net MVC学习总结(二)——控制器与动作(Controller And Action)
一.理解控制器 1.1.什么是控制器 控制器是包含必要的处理请求的.NET类,控制器的角色封装了应用程序逻辑,控制器主要是负责处理请求,实行对模型的操作,选择视图呈现给用户. 简单理解:实现了ICon ...
- [译]Asp.net MVC 之 Contorllers(二)
URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...
随机推荐
- 有关java中static关键的重写问题
<Java编程思想>中这样提到“只有普通的方法调用可以是多态的”.说白了,就是静态方法不能实现重写这种多态. JAVA静态方法形式上可以重写(只要子类不加@Override关键字修饰的话, ...
- UVA 11990 ``Dynamic'' Inversion 动态逆序对
``Dynamic'' Inversion Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://uva.onlinejudge.org/index ...
- hdu 5569 matrix dp
matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5569 D ...
- Codeforces Gym 100002 C "Cricket Field" 暴力
"Cricket Field" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1000 ...
- delphi 自动滚动到最底端scroll
自动滚动到最底端scrollUses MSHTML;{$R *.dfm}var ScrollPos: integer=0;procedure TForm1.Button1Click(Sender: ...
- HDU 5235 Friends (2015 Multi-University Training Contest 2 搜索+剪枝)
题目链接:pid=5305">传送门 题意: n个人给定m个关系.每一个关系为x,y表示x,y是朋友.可是可能是online friends,也可能是offline friends. ...
- visual studio 2013 c++ 打开code map 功能
属性->c++ -> Browse Infomation -> Enable Browse Infomation设为true http://msdn.microsoft.com/li ...
- SQL Server 格式化时间format
select format(sysdatetime(),'yyyy-MM-dd HH:mm:ss'); SQL Server 2012才开始有这功能 这种样式很像oracle的to_char(sysd ...
- Log4Net(二)之记录日志到文档详解
原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/log4net-%E4%BA%8C-%E4%B9%8B% ...
- iaas,paas,saas理解
IaaS.PaaS.SaaS多次看到这几个单词,今天仔细看看来, 这几个词和云计算相关:1,iaaS,Hardware-as-a-Service,信息,硬件服务,服务器,存储和网络硬件,网络存储,带 ...