我已经在ASP.NET MVC site in a way like suggests this post中实现了错误处理。
有了404个错误,一切都能正常工作。但是如何正确地为401 error显示用户友好的屏幕?它们通常不会抛出可以在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的最佳实践吗?
谢谢。
https://stackoverflow.com/questions/1679881
复制相似问题