How to: Construct URLs from Routes ASP.NET routing enables you to programmatically generate URLs that are based on route definitions. You specify parameter values and if it is required a route name, and ASP.NET generates a URL string that corresponds to a matching route. If you change route URL patterns, you do not have to change the code or markup that generates URLs unless the parameters in the patterns are changed. For more information about routing, see ASP.NET Routing.

Creating a URL Based on a Matching Route
When ASP.NET routing constructs a URL, it uses the first route that has a pattern that matches the parameters that you provide, unless you specify a route name. If you specify a route name, ASP.NET uses the named route. The following procedures describe how to create a URL by using code and by using markup. To create a URL by using codeTo create a URL by using markupCreate a RouteUrl expression that contains the parameter names and values that you want to include in the URL. The parameter values must include values that match a route pattern. If you include extra values, those values are assigned to the query string. The following example shows a RouteUrl expression that specifies one parameter.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:year=2007%>">
Data for 2007</asp:HyperLink>
The following example shows a RouteUrl expression that specifies multiple parameters.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:year=2007, locale=CA%>">
Data for California (2007)</asp:HyperLink>
The following example shows a RouteUrl expression that specifies multiple parameters and a route name.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:year=2007, locale=CA,PathName=%>">
Data for California (2007)</asp:HyperLink>
Note |
|---|
The RouteUrl expression can be used only in the markup for a server control. |

Example
The following example shows how to create a URL from a route by using code. The example assumes that you have added two routes to the ASP.NET application. The first route is named SalesRoute and defines the pattern SalesReport/{locale}/{year}. The second route is named ExpensesRoute and defines the pattern ExpensesReport/{locale}/{year}/{querystring}. For more information, see How to: Define Routes for Web Forms Applications. The example shows how to specify the name of the route when you construct the URL. Both SalesRoute and ExpensesRoute take the parameters year and locale. Therefore, you specify the route that you want by using a name. It is not necessary to specify a name if the list of parameters is sufficient to identify the intended route. The example also shows how to include an extra parameter when you construct the URL. The category parameter will be included as a query-string value when the URL is constructed because there no placeholder named category in the URL pattern for the route.
Dim parameters As RouteValueDictionary
parameters = New RouteValueDictionary(New With _
{.locale = "CA", .year = "2008"})
Dim vpd As VirtualPathData
vpd = RouteTable.Routes.GetVirtualPath(Nothing, "ExpensesRoute", parameters)
HyperLink6.NavigateUrl = vpd.VirtualPath
RouteValueDictionary parameters =
new RouteValueDictionary
{
{"locale", "CA" },
{ "year", "2008" } ,
{ "category", "recreation" }
};
VirtualPathData vpd =
RouteTable.Routes.GetVirtualPath(null, "ExpensesRoute", parameters);
HyperLink6.NavigateUrl = vpd.VirtualPath;

See Also
|
方法: ルートから URL を構築する ASP.NET ルーティングでは、ルート定義に基づく URL をプログラムで生成できます。 パラメーター値および必要に応じてルート名を指定すると、一致するルートに対応する URL 文字列が ASP.NET によって生成されます。 ルートの URL パターンを変更した場合でも、パターンに含まれるパラメーターが変わらなければ、URL を生成するコードやマークアップを変更する必要はありません。 ルーティングの詳細については、「ASP.NET ルーティング」を参照してください。

一致するルートに基づく URL の作成
ASP.NET ルーティングによる URL 構築では、ルート名を指定しない限り、指定したパラメーターに一致するパターンを持つ最初のルートが使用されます。 ルート名を指定した場合は、そのルートが ASP.NET によって使用されます。 次の手順では、URL を作成する方法を、コードを使用する場合とマークアップを使用する場合について説明します。 コードを使用して URL を作成するにはマークアップを使用して URL を作成するにはURL に含めるパラメーターの名前と値を含む RouteUrl 式を作成します。 パラメーターの値は、ルートのパターンに一致する値を含んでいる必要があります。 他の値を含めた場合、それらの値はクエリ文字列に割り当てられます。 1 つのパラメーターを指定する RouteUrl 式を次の例に示します。
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:year=2007%>">
Data for 2007</asp:HyperLink>
複数のパラメーターを指定する RouteUrl 式を次の例に示します。
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:year=2007, locale=CA%>">
Data for California (2007)</asp:HyperLink>
複数のパラメーターとルート名を指定する RouteUrl 式を次の例に示します。
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$RouteUrl:year=2007, locale=CA,PathName=%>">
Data for California (2007)</asp:HyperLink>
メモ |
|---|
RouteUrl 式は、サーバー コントロールに対するマークアップでのみ使用できます。 |

使用例
次の例では、コードを使用してルートから URL を作成する方法を示します。 この例は、ASP.NET アプリケーションに 2 つのルートが追加されていることを想定しています。 1 つ目のルートは SalesRoute という名前で、SalesReport/{locale}/{year} というパターンを定義します。 2 つ目のルートは ExpensesRoute という名前で、ExpensesReport/{locale}/{year}/{querystring} というパターンを定義します。 詳細については、「方法: Web フォーム アプリケーションのルートを定義する」を参照してください。 この例は、URL の構築時にルートの名前を指定する方法を示しています。 SalesRoute も ExpensesRoute も、year パラメーターと locale パラメーターを受け取ります。 そのため、名前を使用して目的のルートを指定します。 パラメーターのリストだけで目的のルートを識別できる場合は、名前を指定する必要はありません。 この例は、URL の構築時に追加のパラメーターを指定する方法も示しています。 category パラメーターは、URL を構築するときにクエリ文字列値として組み込まれます。これは、ルートの URL パターンに category という名前のプレースホルダーがないためです。
Dim parameters As RouteValueDictionary
parameters = New RouteValueDictionary(New With _
{.locale = "CA", .year = "2008"})
Dim vpd As VirtualPathData
vpd = RouteTable.Routes.GetVirtualPath(Nothing, "ExpensesRoute", parameters)
HyperLink6.NavigateUrl = vpd.VirtualPath
RouteValueDictionary parameters =
new RouteValueDictionary
{
{"locale", "CA" },
{ "year", "2008" } ,
{ "category", "recreation" }
};
VirtualPathData vpd =
RouteTable.Routes.GetVirtualPath(null, "ExpensesRoute", parameters);
HyperLink6.NavigateUrl = vpd.VirtualPath;

参照
|