mvc(5)——URL路由_3_约束路由

版权声明:本文为博主原创文章,未经博主允许不得转载。本人观点或有不当之处,请在评论中及时指正,我会在第一时间内修改。 https://blog.csdn.net/aiming66/article/details/81610322

  URL模式在如何进行片段匹配方面是保守的,而在如何进行片段内容匹配方
面又是宽松的。
  前两个博客己经解释了对保守程度进行控制的不同技术一一用默认值、可选变量等使路由匹配或多或少的片段数。
  接下来该是了解宽松机制的时候了,看看如何对URL片段内容匹配方面的宽松性进行控制一一即如何进行约束对路由进行匹配的URL。一旦有了对路由行为这两方面(指保守性和宽松性)的控制,就可以创建非常完美的URL方案了。

1、用正则表达式约束路由

先来看一段代码吧。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace urlAndRoutes
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            //用正则表达式
            routes.MapRoute("Myroute", "{controller}/{action}/{id}/{*catchall}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new { Controller="^H.*"},
                new[] { "urlAndRoutes.Controllers" }

                );
        }
    }
}

通过把约束作为参数传递给MapRoute方法,可以定义约束。同默认值一样,约束被表示成一个匿名类型,该类型的属性对应于想要进行约束的片段变量名。上面代码以正则表达式形成了一个约束,它只匹配controller变量值以”H”字母打头的URL

2、将一条路由约束到一组指定的值

可以用正则表达式来约束一条路由,以便对于一个URL片段,只有指定的一些值才会形成匹配。可以用竖线(l)字符来做这件事。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace urlAndRoutes
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            //用正则表达式将一条路由约束到一组指定的值
            routes.MapRoute("Myroute", "{controller}/{action}/{id}/{*catchall}",
                new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                new { Controller = "^H.*",Action="^Index$|^About$" },
                new[] { "urlAndRoutes.Controllers" }

                );
        }
    }
}

  这条约束将允许这条路由只匹配action片段的值是”Index”或“About”的URL。
这条约束合起来就是,施加于action变量的约束与施加于controller变量的约束相组合。这意味着路由将只匹配这样的URL:controller变量以”H”字母打头,而且action变量是”Index”或”About”。因此,现在你应该能明白,前面所说的“创建十分精确的路由”的含义了。

3、用HTTP方法约束路由

含义就是对路由进行约束,从而只能匹配指定的http方法进行匹配的url。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace urlAndRoutes
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {

            //用HTTP方法约束路由
            routes.MapRoute("Myroute", "{controller}/{action}/{id}/{*catchall}",
               new { controller = "Home", action = "Index", id = UrlParameter.Optional },
               new { Controller = "^H.*", Action = "^Index$|^About$",httpmethod=new HttpMethodConstraint("Get")},
               new[] { "urlAndRoutes.Controllers" }

               );
        }
    }
}

上述清单将这条路由限制到GET请求,但可以很容易地添加对其他方法的支持,如下所示:

httpmethod=new HttpMethodConstraint("Get","POST")

4、使用类型和值约束

MVC框架包含了许多内置的约束,它们可以用来限制路由匹配基于类型和片段变量值的URL。例如:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Routing.Constraints;//千万别忘记引用我哦!!
using System.Web.Routing;

namespace urlAndRoutes
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            //使用类型和 值进行约束
            routes.MapRoute("Myroute", "{controller}/{action}/{id}/{*catchall}",
              new { controller = "Home", action = "Index", id = UrlParameter.Optional },
              new { Controller = "^H.*", Action = "^Index$|^About$", httpmethod = new HttpMethodConstraint("Get"),id=new RangeRouteConstraint(10,20) },
              new[] { "urlAndRoutes.Controllers" }

              );
        }
    }
}

在System.web.Mvc.Routing.Constraints命名空间的约束类中,检查片段变量是否是不同的C#类型值,并执行基本的检查。在上面代码中,使用了RangeRouteConstraint类,它检查提供给片段变量的值是在两个边界之间的一个有效int类型值,本例是19和20。其他类型的约束详见下表:

名称 描述
AlphaRouteConstraint() 匹配字母字符,主要是(A一Z,a—z)
BoolRouteConstraint() 匹配一个可以解析成bool类型的值
DateTimeRouteConstraint() 匹配一个可以解析成DateTime类型的值
DecimalRouteConstraint() 匹配一个可以解析成decimal类型的值
DoubleRouteConstraint() 匹配一个可以解析成double类型的值
FloatRouteConstraint() 匹配一个可以解析成float类型的值
IntRouteConstraint() 匹配一个可以解析成int类型的值
LengthRouteConstraint(len) 或者LengthRouteConstraint(min,max) 匹配一个指定字符个数的值,或匹配字符个数在min和max之间的值
LongROuteConStraint() 匹配一个可以解析成long类型的值
MaxRouteConstraint(Val) 匹配一个值小于val的int值
MaxLengthRouteConstraint(len) 匹配一个长度不超过len的字符串
MinRouteConstraint(Val) 匹配一个值大于val的int值
MinLengthRouteConstraint(len) 匹配一个长度至少为len的字符串
RangeRouteConstraint(min,max) 匹配一个值在min和max之间的值

源码下载:https://download.csdn.net/download/aiming66/10599459

猜你喜欢

转载自blog.csdn.net/aiming66/article/details/81610322