これがかなり混乱を招くように聞こえる場合は、事前に謝罪してください。喜んで明確にします。
に以下を追加し、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