3

拡張子がなく、ファイル システムに存在しないパスに HTTPContext.RewritePath を使用しています。カスタム 404 ページを返してほしい。Windows 7 で IIS 7.5、.Net 4、および統合パイプラインを使用しています。

書き換えは、RewriteExampleModule という HTTPModule で行われます。以下に示すように。

using System;
using System.Web;

public class RewriteExampleModule : IHttpModule
{
    public RewriteExampleModule()
    {
    }

    private void context_PostAuthorizeRequest(object sender, EventArgs e)
    {
        HttpContext context = ((HttpApplication)sender).Context;
        String path = context.Request.RawUrl;
        if (path.IndexOf("/edit/") > -1)
        {
            path = path.Replace("/edit/", "/");
            context.RewritePath(path);
        }
    }
    public bool IsReusable
    {
        get { return true; }
    }

    public void Dispose()
    {

    }

    public void Init(HttpApplication context)
    {
        context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest);
    }
}

モジュールセクションで次のように構成されています。

<modules runAllManagedModulesForAllRequests="true">
  <add name="RewriteExampleModule" type="RewriteExampleModule" />
</modules>

/asdf が存在しない /edit/asdf へのリクエストの結果は 500 エラーです。404 である必要がありますが、そうではありません。

続く、

IIS 7.5 – Windows Server 2008 R2 SP1 適用後の拡張子のない URI 処理の重大な変更?

ExtensionlessUrlHandler を削除したところ、404 が返されました。しかし、応答の本文は空です。以下に示す書き換えモジュールと合わせて、完全な web.config を作成します。

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="true" />
    <modules runAllManagedModulesForAllRequests="true">
      <add name="RewriteExampleModule" type="RewriteExampleModule" />
    </modules>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
    </handlers>
    <httpErrors existingResponse="Replace" errorMode="Custom">
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/404error.aspx" responseMode="ExecuteURL" />
    </httpErrors>
  </system.webServer>
</configuration>

この構成では、への呼び出し

/編集/asdf

/asdf が存在しない場合、404 ステータス コードが返されますが、httpErrors の下の web.config で指定されているカスタム コンテンツは返されません。

404 本体が空なのはなぜですか? /edit/ を書き換えない他のパスは問題ありません。

4

0 に答える 0