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 >= 1900) && (year <= 2100)) 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 >= 1) && (month <= 12)) 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 < 1) return false; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: if (day <= 31) return true; break; case 2: if (day <= 28) return true; break; case 4: case 6: case 9: case 11: if (day <= 30) 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哈哈~