Επεξεργασία

Κοινή χρήση μέσω


Route Class

Definition

Provides properties and methods for defining a route and for obtaining information about the 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
Inheritance
Derived

Examples

The following example shows how to create a Route object and add it to the Routes property.

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

Remarks

The Route class enables you to specify how routing is processed in an ASP.NET application. You create a Route object for each URL pattern that you want to map to a class that can handle requests that correspond to that pattern. You then add the route to the Routes collection. When the application receives a request, ASP.NET routing iterates through the routes in the Routes collection to find the first route that matches the pattern of the URL.

Set the Url property to a URL pattern. The URL pattern consists of segments that come after the application name in an HTTP request. For example, in the URL http://www.contoso.com/products/show/beverages, the pattern applies to products/show/beverages. A pattern with three segments, such as {controller}/{action}/{id}, matches the URL http://www.contoso.com/products/show/beverages. Each segment is delimited by the / character. When a segment is enclosed in braces ({ and }), the segment is referred to a URL parameter. ASP.NET routing retrieves the value from the request and assigns it to the URL parameter. In the previous example, the URL parameter action is assigned the value show. If the segment is not enclosed in braces, the value is treated as a literal value.

Set the Defaults property to a RouteValueDictionary object that includes values that are used if a parameter is missing from the URL or to set additional values that are not parameterized in the URL. Set the Constraints property to a RouteValueDictionary object that contains values that are either regular expressions or IRouteConstraint objects. These values are used to determine whether a parameter value is valid.

Constructors

Route(String, IRouteHandler)

Initializes a new instance of the Route class, by using the specified URL pattern and handler class.

Route(String, RouteValueDictionary, IRouteHandler)

Initializes a new instance of the Route class, by using the specified URL pattern, default parameter values, and handler class.

Route(String, RouteValueDictionary, RouteValueDictionary, IRouteHandler)

Initializes a new instance of the Route class, by using the specified URL pattern, default parameter values, constraints, and handler class.

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

Initializes a new instance of the Route class, by using the specified URL pattern, default parameter values, constraints, custom values, and handler class.

Properties

Constraints

Gets or sets a dictionary of expressions that specify valid values for a URL parameter.

DataTokens

Gets or sets custom values that are passed to the route handler, but which are not used to determine whether the route matches a URL pattern.

Defaults

Gets or sets the values to use if the URL does not contain all the parameters.

RouteExistingFiles

Gets or sets a value that indicates whether ASP.NET routing should handle URLs that match an existing file.

(Inherited from RouteBase)
RouteHandler

Gets or sets the object that processes requests for the route.

Url

Gets or sets the URL pattern for the route.

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetRouteData(HttpContextBase)

Returns information about the requested route.

GetType()

Gets the Type of the current instance.

(Inherited from Object)
GetVirtualPath(RequestContext, RouteValueDictionary)

Returns information about the URL that is associated with the route.

MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ProcessConstraint(HttpContextBase, Object, String, RouteValueDictionary, RouteDirection)

Determines whether a parameter value matches the constraint for that parameter.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also