Route 类

定义

提供用于定义路由和获取有关路由的信息的属性和方法。

public ref class Route : System::Web::Routing::RouteBase
public class Route : System.Web.Routing.RouteBase
type Route = class
    inherit RouteBase
Public Class Route
Inherits RouteBase
继承
派生

示例

以下示例演示如何创建对象 Route 并将其添加到 Routes 属性。

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

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
         "Category/{action}/{categoryName}"
         , new CategoryRouteHandler()
    ));
}
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 categoryRoute As Route
    
    urlPattern = "Category/{action}/{categoryName}"
    
    categoryRoute = New Route(urlPattern, New CategoryRouteHandler)
    
    routes.Add(categoryRoute)
End Sub

注解

通过类Route可以指定如何在 ASP.NET 应用程序中处理路由。 为要映射到可处理对应于该模式的请求的类的每个 URL 模式创建一个 Route 对象。 然后,将路由添加到 Routes 集合。 当应用程序收到请求时,ASP.NET 路由循环访问集合中的Routes路由,以查找与 URL 模式匹配的第一个路由。

Url 属性设置为 URL 模式。 URL 模式由 HTTP 请求中的应用程序名称之后的段组成。 例如,在 URL http://www.contoso.com/products/show/beverages中,模式适用于 products/show/beverages。 具有三个段(例如 {controller}/{action}/{id})的模式与 URL http://www.contoso.com/products/show/beverages匹配。 每个段由 / 字符分隔。 当段括在大括号 ({}) 时,该段将引用 URL 参数。 ASP.NET 路由从请求中检索值,并将其分配给 URL 参数。 在前面的示例中,URL 参数 action 分配了值 show。 如果未将段括在大括号中,则将该值视为文本值。

Defaults 属性设置为一个 RouteValueDictionary 对象,其中包含在 URL 中缺少参数时使用的值,或设置 URL 中未参数化的其他值。 将 Constraints 属性设置为包含 RouteValueDictionary 正则表达式或 IRouteConstraint 对象的值的对象。 这些值用于确定参数值是否有效。

构造函数

Route(String, IRouteHandler)

使用指定的 URL 模式和处理程序类初始化 Route 类的新实例。

Route(String, RouteValueDictionary, IRouteHandler)

通过使用指定的 URL 模式、默认参数值和处理程序类,初始化 Route 类的新实例。

Route(String, RouteValueDictionary, RouteValueDictionary, IRouteHandler)

使用指定的 URL 模式、默认参数值、约束和处理程序类初始化 Route 类的新实例。

Route(String, RouteValueDictionary, RouteValueDictionary, RouteValueDictionary, IRouteHandler)

使用指定的 URL 模式、默认参数值、约束、自定义值和处理程序类初始化 Route 类的新实例。

属性

Constraints

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

DataTokens

获取或设置传递到路由处理程序但未用于确定该路由是否与 URL 模式匹配的自定义值。

Defaults

获取或设置要在 URL 不包含所有参数时使用的值。

RouteExistingFiles

获取或设置一个值,它指示 ASP.NET 路由是否应处理与现有文件匹配的 URL。

(继承自 RouteBase)
RouteHandler

获取或设置处理路由的请求的对象。

Url

获取或设置路由的 URL 模式。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetRouteData(HttpContextBase)

返回有关所请求路由的信息。

GetType()

获取当前实例的 Type

(继承自 Object)
GetVirtualPath(RequestContext, RouteValueDictionary)

返回与路由关联的 URL 的相关信息。

MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ProcessConstraint(HttpContextBase, Object, String, RouteValueDictionary, RouteDirection)

确定参数值是否与该参数的约束匹配。

ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅