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 路由进阶(之二)--自定义路由约束的更多相关文章

  1. ASP.NET MVC简单编程之(二)经典路由篇

    话题:请求从路由开始 在实际的ASP.NET MVC开发中,URL访问规则----路由的定义是非常重要的.因为任何一个请求都离不开路由.理解它,我们将能理解MVC处理请求的整个过程,灵活地定义系统各种 ...

  2. Asp.net mvc 知多少(二)

    本系列主要翻译自<ASP.NET MVC Interview Questions and Answers >- By Shailendra Chauhan,想看英文原版的可访问http:/ ...

  3. ASP.NET MVC案例教程(二)

    ASP.NET MVC案例教程(二) 让第一个页面跑起来 现在,我们来实现公告系统中的第一个页面——首页.它非常简单,只包括所有公告分类的列表,并且每个列表项是一个超链接.其中分类数据是用我们的Moc ...

  4. MVC系列——MVC源码学习:打造自己的MVC框架(三:自定义路由规则)

    前言:上篇介绍了下自己的MVC框架前两个版本,经过两天的整理,版本三基本已经完成,今天还是发出来供大家参考和学习.虽然微软的Routing功能已经非常强大,完全没有必要再“重复造轮子”了,但博主还是觉 ...

  5. ASP.NET MVC学习笔记(二)笔记

    接下来我们一起了解ASP.NET MVC的最重要的核心技术,了解ASP.NET MVC的开发框架,生命周期,技术细节. 一.Routing与ASP.NET MVC生命周期 1.Routing——网址路 ...

  6. ASP.NET MVC 5 入门-2控制器、路由

    一.创建项目: 上起始页,选择新项目. 在中新的项目对话框中,右侧语言类别选择C# ,然后项目类型选择Web,然后选择ASP.NET Web 应用程序 (.NET Framework) 项目模板. 将 ...

  7. asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证

    原文:asp.net mvc源码分析-DefaultModelBinder 自定义的普通数据类型的绑定和验证 在前面的文章中我们曾经涉及到ControllerActionInvoker类GetPara ...

  8. Asp.Net MVC学习总结(二)——控制器与动作(Controller And Action)

    一.理解控制器 1.1.什么是控制器 控制器是包含必要的处理请求的.NET类,控制器的角色封装了应用程序逻辑,控制器主要是负责处理请求,实行对模型的操作,选择视图呈现给用户. 简单理解:实现了ICon ...

  9. [译]Asp.net MVC 之 Contorllers(二)

    URL路由模块 取代URL重写 路由请求 URL路由模块的内部结构 应用程序路由 URL模式和路由 定义应用程序路由 处理路由 路由处理程序 处理物理文件请求 防止路由定义的URL 属性路由 书接上回 ...

随机推荐

  1. Android访问WebService的两种方法

    首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...

  2. Codeforces Round #333 (Div. 2) B. Approximating a Constant Range st 二分

    B. Approximating a Constant Range Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  3. 【Android 应用开发】Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载

    19af543b068bdb7f27787c2bc69aba7f Additional Download (32-, 64-bit) Package r10 STL debug info androi ...

  4. 【Android】利用服务Service创建标题栏通知

    创建标题栏通知的核心代码 public void CreateInform() { //定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等) Inten ...

  5. 关于IE8中使用Jquery load方法无法正常加载页面

    最近发现,在IE8中使用Jquery load方法时无法正常加载页面,页面显示空白,没有加载.调试发现,页面多了一个</div>标签,但在FF和CH下表现正常.希望能给遇到同样问题的码农有 ...

  6. iOS小结

    一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始等待用户的操作,自动释放 ...

  7. Xcode无法设置视图的 autosizing control原因

    转自:Xcode无法设置视图的 autosizing control原因 学习Xcode的iOS编程时,可能会发现Autosizing Control不见了,其原因很简单,因为你在设置中选择了Auto ...

  8. css笔记19:浮动的案例

    案例一: 1. 首先是01.html文件: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &q ...

  9. 如何使用数据卷在宿主机和docker容器之间共享文件

    共享宿主机的目录给容器 docker run -i -t -v ~/download:/home/hello python3-env /bin/bash -v  表示创建一个数据卷并挂载到容器里 ~/ ...

  10. 不支持关键字:metadata

    将 string sqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cos ...