路由配置3

1.创建路由约束

你可以使用路由约束来限制匹配特定路由的浏览器请求。可以使用正则表达式来指定一个路由约束。

举个例子,假设你已经在Global.asax文件中定义了一个路由。

代码清单1 - Global.asax.cs

routes.MapRoute(
    "Product",
    "Product/{productId}",
    new {controller="Product", action="Details"}
);

代码清单1包含一个叫做Product的路由。你可以使用Product路由将浏览器请求映射到代码清单2中的ProductController。

代码清单2 - ControllersProductController.cs

using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Details(int productId)
        {
            return View();
        }
    }
}

注意到Product控制器公布的Details()动作接受一个叫做productId的参数。这个参数是一个整数参数。

定义在代码清单1中的路由将会匹配下面的任意URL:

  • /Product/23
  • /Product/7

不幸的是,路由也会匹配下面的URL:

  • /Product/blah
  • /Product/apple

因为Details()动作期望的是一个整数值,发起一个含有非整数值的请求将会导致错误。举个例子,如果你在浏览器中输入/Product/apple URL,那么你将会得到图1所示的错误页。

图1:错误页

你实际想做的是只匹配包含合适整数productId的URL。当定义路由来限制与路由相匹配的URL时,你可以使用约束。代码3中的修改后的Product路由包含了一个正则表达式,它限制了只匹配数字。

代码清单3 - Global.asax.cs

routes.MapRoute(
    "Product",
    "Product/{productId}",
    new {controller="Product", action="Details"},
    new {productId = @"d+" }
 );

正则表达式d+匹配一个或多个整数。这个限制使得Product路由匹配了下面的URL:

  • /Product/3
  • /Product/8999

但是不匹配下面的URL:

  • /Product/apple
  • /Product

这些浏览器请求将由另外的路由处理,或者,如果没有匹配的路由,将会返回一个“The resource could not be found”错误。

2. 创建一个自定义路由约束

这篇教程的目标是演示如何创建一个自定义路由约束。自定义路由约束允许你阻止某个路径被匹配,除非满足一些自定义的条件。

在这篇教程中,我们创建了一个Localhost路由约束。Localhost路由约束只匹配本地计算机发出的请求。通过互联网发出的远程请求不会被匹配。

你可以通过实现IRouteConstraint接口来实现一个自定义路由。这是一个极其简单的接口,它只描述了一个方法:

bool Match(
    HttpContextBase httpContext,
    Route route,
    string parameterName,
    RouteValueDictionary values,
    RouteDirection routeDirection
)

这个方法返回一个布尔值。如果返回了false,与约束相关联的路由将不会匹配浏览器请求。

Localhost约束包含在了代码清单1中。

代码清单1 - LocalhostConstraint.cs

using System.Web;
using System.Web.Routing;

namespace MvcApplication1.Constraints
{
    public class LocalhostConstraint : IRouteConstraint
    {
        public bool Match
            (
                HttpContextBase httpContext, 
                Route route, 
                string parameterName, 
                RouteValueDictionary values, 
                RouteDirection routeDirection
            )
        {
            return httpContext.Request.IsLocal;
        }

    }
}

代码清单1中的约束利用了HttpRequest类公布的IsLocal属性。当发出请求的IP地址是127.0.0.1或者与服务器的IP地址相同时,这个属性返回true。

你在定义于Global.asax的路由中使用了自定义约束。代码清单2中的Global.asax文件使用了Localhost约束来阻止任何人请求Admin页面,除非他们从本地服务器发出请求。举个例子,当请求来自远程服务器时,对于/Admin/DeleteAll的请求将会失败。

代码清单2 - Global.asax

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

namespace MvcApplication1
{

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes){
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            routes.MapRoute(
                "Admin",
                "Admin/{action}",
                new {controller="Admin"},
                new {isLocal=new LocalhostConstraint()}
            );

            //routes.MapRoute(
            //    "Default",                                              // Route name
            //    "{controller}/{action}/{id}",                           // URL with parameters
            //    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            //);
        }

        protected void Application_Start(){
            RegisterRoutes(RouteTable.Routes);
        }
    }
}
View Code

Localhost约束使用在了Admin路由的定义中。这个路由不会被远程浏览器请求所匹配。然而,应该意识到定义在Global.asax中的其他路由可能会匹配相同的请求。理解这一点很重要:约束阻止了特定路由匹配某一请求,而不是所有定义在Global.asax文件中的路由。

注意到Default路由在代码清单2中的Glabal.asax文件中被注释掉了。如果你包含Default路由,那么Default路由将会匹配对Admin控制器的请求。在这种情况下,远程用户仍然可以调用Admin控制器的动作,即使他们的请求不匹配Admin路由。

原文地址:https://www.cnblogs.com/tianma3798/p/4245676.html