Route.Constraints 属性

定义

获取或设置表达式字典,用于指定 URL 参数的有效值。

public:
 property System::Web::Routing::RouteValueDictionary ^ Constraints { System::Web::Routing::RouteValueDictionary ^ get(); void set(System::Web::Routing::RouteValueDictionary ^ value); };
public System.Web.Routing.RouteValueDictionary Constraints { get; set; }
member this.Constraints : System.Web.Routing.RouteValueDictionary with get, set
Public Property Constraints As RouteValueDictionary

属性值

包含参数名称和表达式的对象。

示例

以下示例演示如何创建 Route 对象并设置 ConstraintsDataTokensDefaults 属性。

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
    reportRoute.Defaults = new RouteValueDictionary { { "locale", "en-US" }, { "year", DateTime.Now.Year.ToString() } };
    reportRoute.Constraints = new RouteValueDictionary { { "locale", "[a-z]{2}-[a-z]{2}" }, { "year", @"\d{4}" } };
    reportRoute.DataTokens = new RouteValueDictionary { { "format", "short" } };
    routes.Add(reportRoute);
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim urlPattern As String
    Dim reportRoute As Route
    
    urlPattern = "{locale}/{year}"
    
    reportRoute = New Route(urlPattern, New ReportRouteHandler)
    reportRoute.Defaults = New RouteValueDictionary(New With {.locale = "en-US", .year = DateTime.Now.Year.ToString()})
    reportRoute.Constraints = New RouteValueDictionary(New With {.locale = "[a-z]{2}-[a-z]{2}", .year = "\d{4}"})
    reportRoute.DataTokens = New RouteValueDictionary(New With {.format = "short"})

    routes.Add(reportRoute)
End Sub

下面的示例演示一个 Route 对象,其 Constraints 属性包含名为 httpMethod 的参数,以及 值的 类的 HttpMethodConstraint 实例。

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    string[] allowedMethods = { "GET", "POST" };
    HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods);

    Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
    reportRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } };

    routes.Add(reportRoute);
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    RegisterRoutes(RouteTable.Routes)
End Sub

Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
    Dim urlPattern As String
    Dim reportRoute As Route
    Dim allowedMethods() As String = {"GET", "POST"}
    Dim methodConstraints As HttpMethodConstraint  
    
    methodConstraints = New HttpMethodConstraint(allowedMethods)
    
    Dim constraintValues = New With {.httpMethod = methodConstraints}
    
    urlPattern = "{locale}/{year}"
    
    reportRoute = New Route(urlPattern, New ReportRouteHandler)
    reportRoute.Constraints = New RouteValueDictionary(constraintValues)
    
    routes.Add(reportRoute)
End Sub

注解

通过 Constraints 属性,您可以限制路由处理的 URL 参数的可接受值。 将 对象分配给 RouteValueDictionary 属性 Constraints 。 对象中的每个 RouteValueDictionary 元素都包含参数的名称和下列项之一:

  • 定义正则表达式的字符串。 正则表达式不区分大小写。

  • 一个实现 IRouteConstraint 接口和包含 方法的对象 Match

HttpMethodConstraint 类实现 IRouteConstraint 接口。 可以包含 类的 HttpMethodConstraint 实例作为约束,以指定路由可接受的 HTTP 谓词。

适用于

另请参阅