Partager via


How to: Access URL Parameters in a Routed Page

In an ASP.NET Web forms application that uses routing, some segments of the URL can contain variable information. A request handler such as an .aspx page receives this information as a collection of key-value pairs, similar to how it receives query string data. For query strings, both the keys and the values are in the URL. For routes, the keys are the placeholder names that are defined in the URL pattern, and only the values are in the URL. You can access the collection that contains URL parameter values by using code or markup.

For more information about routing and URL patterns, see ASP.NET Routing.

Accessing URL Parameters in a Routed Page

To access URL parameters by using code

  • Retrieve the value from the RouteData object and cast it to the appropriate type.

    The RouteData object is available in the RouteData property of the Page object. The following example shows how to retrieve a route parameter and convert it to an integer value.

    Dim year As Integer = Convert.ToInt32(Page.RouteData.Values("year"))
    
    int year = Convert.ToInt32(Page.RouteData.Values["year"])
    

To access URL parameters by using markup

  • Create a RouteValue expression that indicates which parameter value that you want to access.

    The following example shows a Label that uses a RouteValue expression in its Text property.

    <asp:Label ID="Label1" runat="server"
      Text="<%$RouteValue:year%>" />
    

    Note

    The RouteValue expression can be used only in the markup for a server control.

See Also

Tasks

Walkthrough: Using ASP.NET Routing in a Web Forms Application

Reference

System.Web.Routing.RouteData

Page.RouteData

Concepts

ASP.NET Routing