MVC (4) でエラー処理をセットアップしましたが、うまく機能します。私は、global.asax に HandleErrorAttribute を登録し、web.config で適切な構成を設定しました。ただし、エラー ビューにリダイレクトし、エラー ビュー自体がエラーをスローすると、エラー ページにリダイレクトされます。レイアウトやアプリ外で管理しているレイアウトでエラーが発生しています。レイアウトに誤りがあれば困ります。どうすればこれを防ぐことができますか? どのような種類のエラー処理フォールバックを使用する必要がありますか? 別のレイアウトを使用することはできません。
1 に答える
1
これが私のやり方です。試してみる:
protected void Application_Error(object sender, EventArgs e)
{
//Retrieving the last server error
var exception = Server.GetLastError();
//Erases any buffered HTML output
Response.Clear();
//Declare the exception
var httpException = exception as HttpException;
var routeData = new RouteData();
routeData.Values.Add("controller", "Error"); //Adding a reference to the error controller
if (httpException == null)
{
routeData.Values.Add("action", "ServerError"); //Non HTTP related error handling
}
else //It's an Http Exception, Let's handle it.
{
switch (httpException.GetHttpCode())
{
//these are special views to handle each error
case 401:
case 403:
//Forbidden page.
routeData.Values.Add("action", "Forbidden");
break;
case 404:
//Page not found.
routeData.Values.Add("action", "NotFound");
break;
case 500:
routeData.Values.Add("action", "ServerError");
break;
default:
routeData.Values.Add("action", "Index");
break;
}
}
//Pass exception details to the target error View.
routeData.Values.Add("message", exception);
//Clear the error on server.
Server.ClearError();
//Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData));
}
于 2013-02-27T19:36:00.180 に答える