我已经在 ASP.NET MVC 站点中实现了错误处理,就像这篇文章所建议的那样。
404错误一切正常。但是如何正确显示401 错误的用户友好屏幕?它们通常不会抛出可以在内部处理的异常,Application_Error()
而是操作返回 HttpUnauthorizedResult。一种可能的方法是将以下代码添加到Application_EndRequest()
方法的末尾
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 的最佳实践?
谢谢。