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,如下截图:

ASP.NET MVC 路由进阶(之二)--自定义路由约束

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

ASP.NET MVC 路由进阶(之二)--自定义路由约束

由此可见,当输入非法路由时,路由系统会当做错误处理,所以我们可以为MVC路由设置约束,以规范路由格式。

好了,今天写到这里。希望能对你有所帮助O(∩_∩)O哈哈~

上一篇:Windows Server 2016介绍与安装


下一篇:pydev+python+Eclipse环境搭建+ 调试快捷键汇总