このエントリは「One ASP.NET Advent Calendar 2013 – Adventar」の参加エントリです。
前日は@NAL_6295さんの「NAL-6295の舌先三寸 » Blog Archive » [ASP.NET MVC][C#]Controllerで複数のcheckboxの状態を個々のチェック状態のBooleanではなく、チェックされているものだけの配列を引数で受け取りたい」でした。
Friendly URLsって?
ASP.NET WebFormsでも、シュッとしたURLを簡単に扱うためのライブラリです。
The ASP.NET Friendly URLs library makes it easy to enable extensionless URLs for file-based handlers (e.g. ASPX, ASHX) in ASP.NET applications.
乱暴にまとめると次のような機能を提供してくれます。
- 拡張子なしのURL
- ~/Customer → ~/Customer.aspxにルーティング
- 「セグメント」を使ったパラメーター指定
- ~/Customer/1 → ~/Customer.aspx?id=1のような扱いができる
導入
まずは空のWebアプリケーションプロジェクトを作成しましょう。
「One ASP.NET」なので、「Web Forms」にチェックを入れ、「Empty」テンプレートを選択します。
次にNuGetからFriendlyURLsを追加します。
NuGet Gallery | Microsoft.AspNet.FriendlyUrls 1.0.2
Friendly URLsの有効化
FriendlyURLsを有効にするには、Global.asaxのApplication.Startイベントハンドラーにて、RouteConfigクラスの拡張メソッド、「EnableFriendlyUrls()」を実行します。
そして、そのための処理はNuGetで追加されたApp_Start/RouteConfigクラスのRegisterRoutesメソッドを使います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Web; | |
using System.Web.Routing; | |
using Microsoft.AspNet.FriendlyUrls; | |
namespace FriendlyURLsSample | |
{ | |
public static class RouteConfig | |
{ | |
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
var settings = new FriendlyUrlSettings(); | |
settings.AutoRedirectMode = RedirectMode.Permanent; | |
routes.EnableFriendlyUrls(settings); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Web.Routing; | |
namespace FriendlyURLsSample | |
{ | |
public class Global : System.Web.HttpApplication | |
{ | |
protected void Application_Start(object sender, EventArgs e) | |
{ | |
RouteConfig.RegisterRoutes(RouteTable.Routes); // <- FriendlyURLsを有効に | |
} | |
} | |
} |
遷移元画面作成
遷移元画面として、Default.aspxを作成します。テキストボックスとボタンがそれぞれ一つずつある、シンプルな画面です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="FriendlyURLsSample.Default" %> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Greeting!</title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
Enter your name: | |
<asp:TextBox ID="Name" runat="server"></asp:TextBox> | |
<asp:Button ID="Greet" runat="server" Text="Greet" OnClick="Greet_Click" /> | |
</div> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Microsoft.AspNet.FriendlyUrls; | |
namespace FriendlyURLsSample | |
{ | |
public partial class Default : System.Web.UI.Page | |
{ | |
protected void Greet_Click(object sender, EventArgs e) | |
{ | |
Response.Redirect(FriendlyUrl.Href("~/Greeting", Name.Text)); | |
} | |
} | |
} |
ポイントは、FriendlyUrl.Hrefメソッドです。このメソッドの第一引数に遷移先ページのURL(拡張子なし)、第二引数以降に「セグメント」として引き渡したいパラメーターを設定します。
「セグメント」については後述します。
遷移先画面作成
遷移先画面として、Greeting.aspxを作成します。前の画面で入力した名前を使って、あいさつ文を表示するシンプルな画面です。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Greeting.aspx.cs" Inherits="FriendlyURLsSample.Greeting" %> | |
<!DOCTYPE html> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head runat="server"> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>Greeting!</title> | |
</head> | |
<body> | |
<form id="form1" runat="server"> | |
<div> | |
Hello! <%: Name %> | |
</div> | |
</form> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Microsoft.AspNet.FriendlyUrls; | |
namespace FriendlyURLsSample | |
{ | |
public partial class Greeting : System.Web.UI.Page | |
{ | |
public string Name { get; private set; } | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
Name = FriendlyUrl.Segments.First(); | |
} | |
} | |
} |
この画面のポイントは、FriendlyUrl.Segmentsプロパティです。前の画面のHrefメソッドで「セグメント」に指定したパラメーターが、IList<string>型で格納されています。
今回は前の画面で入力した名前だけが欲しいので、LINQのFirstメソッドで1つだけ取得しています。
実行結果
では実行してみましょう。まず起動すると、名前を入力する画面が開きます。
名前を入力して「Greet」ボタンをクリックすると、次の画面に遷移し、入力した名前を使ってあいさつ文が表示されます。
さて、アドレスバーを見てみましょう。次のアドレスとなっています。
http://localhost:3622/Greeting/TAKANO%20Sho
Friendly URLsの力により、”~Greeting”まででGreeting.aspx画面が表示され、その後の”/”で区切られた値がセグメントとして扱われます。FriendlyUrl.Hrefメソッドで入力した名前をセグメントに指定しているので、入力した”TAKANO Sho”がちゃんとURLエンコードされ、”TAKANO%20Sho”として設定されていることがわかります。
そして、Greeting画面では、Page_Loadイベントハンドらーにて、FriendlyUrl.Segmentsプロパティを使い、上記の”TAKANO%20Sho”というセグメントの値を、URLデコードして、”TAKANO Sho”として扱えていることが確認できます。
他にも……
ASP.NET 4.5で導入されたモデルバインドと組み合わせるための属性が使えたり、モバイルサイト用のページに自動でルーティングしたりといった機能があるようです。前者については、「ASP.NET スキャフォールディング」の中で使われていますので、以下の拙著記事を見てみてください。後者については良く調べていないので、前述のCodePlexのプロジェクトホームを参照してください。
ASP.NET 4.5の「Scaffolding(スキャフォールディング)」機能を試す(前編)(1/5):CodeZine
ASP.NET 4.5の「Scaffolding(スキャフォールディング)」機能を試す(後編)(1/4):CodeZine
CodeZineの記事では、もう少し詳しくFriendly URLsの動作を解説していますので、ぜひご覧ください。
なお、今回作成したサンプルは、GitHubにアップしてあります。
masaru-b-cl/one-asp-net-advent-calendar-2013
次は?
@taiga_takahariさんにバトンタッチです!
ピンバック: 複数のページからPostBackUrlで遷移したい時 | C#よりVB派ですけど何か?
ピンバック: 2013年振り返り by @masaru_b_cl | be free