Export (0) Print
Expand All

How to: Access URL Parameters in a Routed Page

.NET Framework 4

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.

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%>" />
    
    NoteNote

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

Community Additions

ADD
Show:
© 2014 Microsoft