URLルーティングで "." の文字が含まれていると、動作しない現象について紹介します。
現象
ASP.NET Webアプリケーションプロジェクトを作成し、下記のコードを用意します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Routing;
namespace AspNetRoutingDot
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("", "item/{itemid}.html", "~/Default.aspx");
}
protected void Session_Start(object sender, EventArgs e)
{
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AspNetRoutingDot.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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AspNetRoutingDot
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = (string)Page.RouteData.Values["itemid"];
}
}
}
解説
下記のコードでURLルーティングをします。
http://localhost:58512/item/penguin.html
のURLにアクセスすると、
http://localhost:58512/Default.aspx
のURLにルーティングすることを期待したコードです。その際、URLの"penguin"の部分を
itemid
に格納する動作とします。
RouteTable.Routes.MapPageRoute("", "item/{itemid}.html", "~/Default.aspx");
ルーティング先のDefault.aspxページでは、
itemid
の値をLabel1に表示します。
実行結果
上記のプロジェクトを実行します。Webブラウザが表示されますので、アドレスバーに
http://localhost:58512/item/penguin.html
を入力してアクセスします。
期待した動作であれば、ページが表示され、画面に"penguin"の文字列が表示されますが、実際は404のnot foundエラーの画面が表示されます。
"."が含まれていても、正しく動作する例
先のプログラムからGlobal.asax.csのApplication_Start メソッドを下記に書き換えます。
http://localhost:58512/item.active/penguin
のURLにアクセスすると、
http://localhost:58512/Default.aspx
のURLにルーティングするコードです。その際、URLの"penguin"の部分を
itemid
に格納する動作とします。
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("", "item.active/{itemid}", "~/Default.aspx");
}
上記のコードに変更したプロジェクトを実行し、
http://localhost:58512/item.active/penguin
のURLにアクセスします。下図の画面が表示され、ルーティングが動作していることが確認できます。
しかし、
http://localhost:58512/item.active/penguin.html
のURLを入力すると、404 Not Found のエラーとなります。
URLパターンの
{...}
で囲まれた箇所(プレースホルダー)には"."を含めることができないようです。
その他の動作例
例1
下記のURLパターンの場合
RouteTable.Routes.MapPageRoute("", "item-{itemid}-{option}", "~/Default.aspx");
http://localhost:58512/item-penguin-run
は動作します。
http://localhost:58512/item-penguin-run.htm
は404エラーとなります。
例2
下記のURLパターンの場合
RouteTable.Routes.MapPageRoute("", "item.{itemid}.{option}", "~/Default.aspx");
http://localhost:58512/item.penguin.run
は404エラーとなります。
原因
URLパターンは以下の条件を満たす必要があります。
- URLパターンの
{...}
で囲まれた箇所(プレースホルダー)には"."を含めることができない
{...}
で囲まれた箇所(プレースホルダー)は区切り文字で区切る必要がある
- プレースホルダーの区切り文字に"."は指定できない
- プレースホルダーを区切り文字なしに、続けて記述することはできない。(
{item}{option}
など)
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-06
作成日: 2018-01-31