码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2015-07-16 02:05:34      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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哈哈~

 

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

标签:

原文地址:http://www.cnblogs.com/JinvidLiang/p/4644270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!