【发布时间】:2010-12-13 09:16:41
【问题描述】:
我已经在 ASP.NET MVC 站点 in a way like suggests this post 中实现了错误处理。
出现 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 的最佳实践?
谢谢。
【问题讨论】: