翻訳への提案を行います
 
他のユーザーによる提案:

progress indicator
他の提案はありません。
クリックして評価とフィードバックをお寄せください
MSDN
MSDN ライブラリ
Web 開発
ASP.NET
ASP.NET 4
ASP.NET Web プロジェクト
 方法: ルートから URL を構築する
すべて縮小/すべて展開 すべて縮小
コンテンツの表示:   英語と日本語を並べて表示コンテンツの表示: 英語と日本語を並べて表示
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.

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 code

  1. Create an instance of the RouteValueDictionary class that contains all the parameter values that you want to include in the URL. The parameters must match a route URL pattern.

    The RouteValueDictionary object can include parameters in addition to the ones that match a route URL pattern. If it does, those values are assigned to the query string.

  2. Call the GetVirtualPath method of the RouteCollection class and pass the RouteValueDictionary object in order to construct the URL. To indicate a specific route, include the route name.

    The GetVirtualPath method returns a VirtualPathData object.

  3. From the VirtualPathData object, retrieve the VirtualPath property to get the string that represents the URL.

To create a URL by using markup

  • Create 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>
    NoteNote

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

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.

Visual Basic
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
C#
RouteValueDictionary parameters = 
    new RouteValueDictionary  
        { 
            {"locale", "CA" }, 
            { "year", "2008" } , 
            { "category", "recreation" }
        };

VirtualPathData vpd = 
    RouteTable.Routes.GetVirtualPath(null, "ExpensesRoute", parameters);

HyperLink6.NavigateUrl = vpd.VirtualPath;
方法: ルートから URL を構築する

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

ルーティングの詳細については、「ASP.NET ルーティング」を参照してください。

ASP.NET ルーティングによる URL 構築では、ルート名を指定しない限り、指定したパラメーターに一致するパターンを持つ最初のルートが使用されます。 ルート名を指定した場合は、そのルートが ASP.NET によって使用されます。

次の手順では、URL を作成する方法を、コードを使用する場合とマークアップを使用する場合について説明します。

コードを使用して URL を作成するには

  1. URL に含めるすべてのパラメーター値を保持する RouteValueDictionary クラスのインスタンスを作成します。 パラメーターは、ルートの URL パターンに一致する必要があります。

    RouteValueDictionary オブジェクトには、ルートの URL パターンに一致するパラメーターに加えて、それ以外のパラメーターを含めることができます。 この場合、それらの値はクエリ文字列に割り当てられます。

  2. RouteCollection クラスの GetVirtualPath メソッドを呼び出し、RouteValueDictionary オブジェクトを渡して URL を構築します。 特定のルートを指定する場合は、ルート名を含めます。

    GetVirtualPath メソッドは VirtualPathData オブジェクトを返します。

  3. VirtualPathData オブジェクトから VirtualPath プロパティを取得して、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 の構築時にルートの名前を指定する方法を示しています。 SalesRouteExpensesRoute も、year パラメーターと locale パラメーターを受け取ります。 そのため、名前を使用して目的のルートを指定します。 パラメーターのリストだけで目的のルートを識別できる場合は、名前を指定する必要はありません。

この例は、URL の構築時に追加のパラメーターを指定する方法も示しています。 category パラメーターは、URL を構築するときにクエリ文字列値として組み込まれます。これは、ルートの URL パターンに category という名前のプレースホルダーがないためです。

Visual Basic
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
C#
RouteValueDictionary parameters = 
    new RouteValueDictionary  
        { 
            {"locale", "CA" }, 
            { "year", "2008" } , 
            { "category", "recreation" }
        };

VirtualPathData vpd = 
    RouteTable.Routes.GetVirtualPath(null, "ExpensesRoute", parameters);

HyperLink6.NavigateUrl = vpd.VirtualPath;
コミュニティ コンテンツ   コミュニティ コンテンツとは
新しいコンテンツの追加 RSS  注釈
Processing
© 2012 Microsoft. All rights reserved. 使用条件 | 商標 | プライバシー
Page view tracker