Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [87]

By Root 5263 0
of the RouteData Class

Member

Description

DataTokens

List of additional custom values that are passed to the route handler

GetRequiredString

Method that takes the name of a route parameter and returns its value

Route

Returns the current Route object

RouteHandler

Returns the handler for the current route

Values

Returns the dictionary of route parameter values

The following code snippet shows how you retrieve parameters in Page_Load:

protected void Page_Load(object sender, EventArgs e)

{

var action = RouteData.GetRequiredString("action");

...

}

The only difference between using GetRequiredString and accessing the Values dictionary is that GetRequiredString throws if the requested value is not found. In addition, GetRequiredString uses protected access to the collection via TryGetValue instead of a direct reading.

Structure of Routes


A route is characterized by the five properties listed in Table 4-10.

Table 4-10. Properties of the Route Class

Property

Description

Constraints

List of additional constraints the URL should fulfill to match the route.

DataTokens

List of additional custom values that are passed to the route handler. These values, however, are not used to determine whether the route matches a URL pattern.

Defaults

List of default values to be used for route parameters.

RouteHandler

The object responsible for retrieving the HTTP handler to serve the request.

Url

The URL pattern for the route.

Constraints, DataTokens, and Defaults are all properties of type RouteValueDictionary. In spite of the fancy name, the RouteValueDictionary type is a plain dictionary.

Most of the time, the pattern defined by the route is sufficient to decide whether a given URL matches or not. However, this is not always the case. Consider, for example, the situation in which you are defining a route for recognizing requests for product details. You want to make sure of the following two aspects.

First, make sure the incoming URL is of the type http://server/{category}/{productId}, where {category} identifies the category of the product and {productId} indicates the ID of the product to retrieve.

Second, you also want to be sure that no invalid product ID is processed. You probably don’t want to trigger a database call right from the URL routing module, but at the very least, you want to rule out as early as possible any requests that propose a product ID in an incompatible format. For example, if product IDs are numeric, you want to rule out anything passed in as a product ID that is alphanumeric.

Regular expressions are a simple way to filter requests to see if any segment of the URL is acceptable. Here’s a sample route that keeps URLs with a string product ID off the application:

routes.MapPageRoute(

"ProductInfo",

"Category/{category}/{productId}/{locale}",

"~/categories.aspx",

true,

new { category = "Beverages", locale="en-us" },

new { productId = @"\d{8}",

locale = ""[a-z]{2}-[a-z]{2}" }

);

The sixth parameter to the MapPageRoute method is a dictionary object that sets regular expressions for productId and locale. In particular, the product ID must be a numeric sequence of exactly eight digits, whereas the locale must be a pair of two-letter strings separated by a dash. The filter doesn’t ensure that all invalid product IDs and locale codes are stopped at the gate, but at least it cuts off a good deal of work. An invalid URL is presented as an HTTP 404 failure and is subject to application-specific handling of HTTP errors.

More in general, a route constraint is a condition that a given URL parameter must fulfill to make the URL match the route. A constraint is defined via either regular expressions or objects that implement the IRouteConstraint interface.

Preventing Routing for Defined URLs


The ASP.NET URL routing module gives you maximum freedom to keep certain URLs off the routing mechanism. You can prevent the routing system from handling certain URLs in two steps. First, you define a pattern for those URLs and save it to

Return Main Page Previous Page Next Page

®Online Book Reader