4

これは私の最初の投稿であり、この問題の解決策をここや Web 上の他の多くのフォーラムで探しましたが、成功しませんでした。

ディレクトリ アクセス拒否エラー 403.14 のカスタム エラーを作成しようとしています。たとえば、誰かが Web サイトの "_assests" ディレクトリを読み込もうとした場合などです。これを実行したい各ディレクトリに default.aspx ページを追加することで回避できることはわかっていますが、web.config ファイルのタグに似たサイト全体のソリューションがあるかどうか疑問に思っていました

<configuration>
<system.web>
    <customErrors defaultRedirect="/Errors/GenericError.aspx" mode="RemoteOnly">
      <error statusCode="401"
         redirect="/Errors/401.aspx"/>
      <error statusCode="403"
         redirect="/Errors/403.aspx"/>
      <error statusCode="404"
         redirect="/Errors/404.aspx"/>
    <!-- 
      <error statusCode="403.14"
         redirect="/"/>
    -->
    </customErrors>
</system.web>
</configuration>

データ型 Int ではないため、10 進数を含む statusCode を使用できないという web.config をコーディングするときにエラーが発生します。

Server 2008 で IIS 7 を使用しています。

何か案は?

4

2 に答える 2

2

これがかなり混乱を招くように聞こえる場合は、事前に謝罪してください。喜んで明確にします。

に以下を追加し、web.configこれまでのところ動作しているようです。理由はわかりませんが、403403.aspxエラーに対して、そのページではなくカスタム ページにリダイレクトするよう明示的に指示しないGenericError.aspxと、500エラーが発生します。ただし、404エラーをカスタム404.aspxページにリダイレクトすると、GenericError.aspxコードが適切に記述され、期待どおりではなく、実際の404.aspxページにリダイレクトされないように見えます (のコメント部分を参照web.config)。奇妙な。

コード:

web.config ファイル:

<system.webServer>
    <httpErrors existingResponse="Replace" errorMode="Custom">
        <remove statusCode="403"/>
        <remove statusCode="404"/>
        <error statusCode="403" path="/Errors/403.aspx" responseMode="Redirect"  />
<!-- <error statusCode="403" path="/Errors/GenericError.aspx" responseMode="Redirect"  /> -->
<!-- <error statusCode="404" path="/Errors/GenericError.aspx" responseMode="Redirect"  /> -->
<error statusCode="404" path="/Errors/404.aspx" responseMode="Redirect"  />
    </httpErrors>
</system.webServer>

GenericError.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{

    var ex = HttpContext.Current.Server.GetLastError();

    if (ex is HttpException)
    {
        var nex = ex as HttpException;
        //Label in the Main code displays the Error Code from the Error String
        this.customErrorMessageCode.Text += "Error Code" + " " + nex.GetHttpCode().ToString();
        //Label in the Main code displays the Error Message from the Error String
        this.customErrorMessageLabel.Text += ex.Message.ToString();
        //DIV ID in the Main code displays the entire error message
        this.customErrorMessage.Visible = true;
        switch (nex.GetHttpCode())
        {
            case 404:
                this.customErrorMessageCode.Text += " Page Not Found";
                this.customErrorMessageImage.Visible = true;
                // do somehting cool
                break;
            case 403:
                this.customErrorMessageCode.Text += " Forbidden Access";
                this.customErrorMessageImage.Visible = true;
                // do somehting cool
                break;
            case 500:
                this.customErrorMessageCode.Text += " Internal Error";
                this.customErrorMessageImage.Visible = true;
                // do somehting cool
                break;
            default:
                break;
        }
    }
    else {
        this.customErrorMessageLabel.Text += ex.Message + ex.GetType().ToString();
    }

}

ソース:

web.config の CustomError

于 2013-06-03T13:44:06.777 に答える
1

おそらく、を使用して実際のエラー コードに基づくリダイレ​​クト アプローチを使用できますServer.GetLastError()

には、次のGlobal.asaxようなものがあります。

protected void Application_Error(object sender, EventArgs e)
{
    if (Context.IsCustomErrorEnabled) {
        ShowCustomErrorPage(Server.GetLastError());
    }
}

ShowCustomErrorPage次に、HTTP コードを読み取り、正しいエラー ページにリダイレクトする switch ステートメントを使用します。

これはソース リンクからのものですが、MVC 固有すぎる可能性があります。MVCを使用しているとは言わなかったので、想定してやみくもにコピーアンドペーストしたくありませんでした。

私は MVC にあまり詳しくありませんが、ここでの原則は、シナリオに合わせて調整できるように見えます。

ソース: http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-custom-error-pages

編集

役立つ可能性のある StackOverflow の投稿がいくつか見つかりました。

web.config / Global.asax のカスタム エラー処理が存在しないディレクトリを処理しない

カスタム エラー処理 Asp.Net

于 2013-05-31T13:22:46.283 に答える