動作確認環境は次のとおり

  • OS:Vista Enterprise, Widnwos Server 2003
  • IDE: Visual Studio 2008 Professional
  • IIS:IIS6.0, IIS7.0

IISとVSの組み込みコンテナ上のデバッグ実行の両方で確認しています。
URLは/ShowArticle.aspx?ID=【記事ID】と本来アクセスされるページを/ShowArticle/【ID】.aspxのユーザフレンドリな形式でアクセスできるようにします。

1. 書き換えを行うクラスを作成する

クラスを作成し、リクエストされたURLを書き換えます。IHTTPModuleインタフェースを実装するモジュールを作成し、リクエスト処理開始時にリクエストされたURLを書き換えるために、BeginRequestイベントをサブスクライブします。Application_BeginRequestイベントハンドラでURLを書き換える処理を記述しています。URLを書き換える対象の場合は、新しいパスを作成して、HttpContext.RewritePathメソッドを呼び出します。

namespace ComponentGeek.Sample
{
    public class ShowArticleModule : IHttpModule
    {

        #region IHttpModule Members

        void IHttpModule.Dispose()
        {
        }

        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += this.Application_BeginRequest;
            context.PreRequestHandlerExecute += this.Application_PreRequestHandlerExecute;
        }
        #endregion


        private bool rewrited = false;
        private string originalPath = "";
        private void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication application = sender as HttpApplication;
            HttpContext context = application.Context;

            Regex regex = new Regex(@"ShowArticle/(.*)\.aspx", RegexOptions.Singleline);
            Match match = regex.Match(context.Request.Path);
            if (match.Success)
            {
                rewrited = true;
                originalPath = context.Request.Path;
                string path = regex.Replace(context.Request.Path, @"ShowArticle.aspx?ID=$1");
                context.RewritePath(path);
            }
            else
            {
                rewrited = false;
            } } } }

 

2.書き戻す処理の追加

ページが処理する前に書き換えたページのURLをもともとのリクエストされたURLに書き戻します。この処理をしないと、出力されるURLのアプリケーションルートのパスが/ShowArticle/となってしまい、/App_Theme/などのパスが/ShowArticle/App_Theme/などのように出力されてスタイルの設定などがおかしくなってしまいます。理由はだれか教えて下さいm(_ _)m。書き戻しを行うために、HttpContext.PreRequestHandlerExecuteイベントをサブスクライブします。Application_PreRequestHandlerExecuteイベントハンドラで書き換えが発生した場合はURLをオリジナルのリクエストURLに書き戻すように処理を変更しています。

        void IHttpModule.Init(HttpApplication context)
        {
            context.BeginRequest += this.Application_BeginRequest;
            context.PreRequestHandlerExecute += this.Application_PreRequestHandlerExecute;
        }
private void Application_PreRequestHandlerExecute(object sender, EventArgs e) { if (rewrited) { HttpApplication application = sender as HttpApplication; HttpContext context = application.Context; Regex regex = new Regex(@"ShowArticle.aspx"); Match match = regex.Match(context.Request.Path); if (match.Success) { context.RewritePath(originalPath); } } }

3.Web.configに設定

Web.confgのhttpmodulesセクションに記述を追加します。この処理で/ShowArticle/10.aspxというリクエストURLは/ShowArticle/10.aspx?ID=10というURLに書き換えられてShowArticle.aspx上で処理されるようになります。

<httpModules>
      ...
<add name="ShowArticleModule" type="ComponentGeek.Sample.ShowArticleModule"/> </httpModules>

4.まとめ

MSDN Magazine2008年1月号では別のURL書き換え方法が紹介されています。HttpServerUtility.TransferRequest(書き換えたいURL)でURL書き換えができるらしいのですが、動作確認は行っていません。動作要件にIIS7.0以上と.NET3.5以上が必須となっています。詳細はMSDNを確認して下さい。