この投稿を示唆するような方法で、 ASP.NET MVC サイトにエラー処理を実装しました。
404 エラーで、すべて正常に動作します。しかし、 401エラーに対してユーザーフレンドリーな画面をどのように正しく表示するのでしょうか? それらは通常、内部で処理できる例外をスローしませんApplication_Error()
が、アクションは HttpUnauthorizedResult を返します。Application_EndRequest()
考えられる方法の 1 つは、メソッドの最後に次のコードを追加することです。
if (Context.Response.StatusCode == 401)
{
throw new HttpException(401, "You are not authorised");
// or UserFriendlyErrorRedirect(new HttpException(401, "You are not authorised")), witout exception
}
ただしApplication_EndRequest()
、Context.Session == null内でerrorController.Execute()
は、デフォルトの TempDataProvider を使用できないため失敗します。
// Call target Controller and pass the routeData.
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(
new HttpContextWrapper(Context), routeData)); // Additional information: The SessionStateTempDataProvider requires SessionState to be enabled.
では、ASP.NET MVC アプリケーションで 401 を「ユーザー フレンドリーに処理」するためのベスト プラクティスをいくつか提案できますか?
ありがとう。