ASP.NET MVC用户友好的401错误 [英] ASP.NET MVC user friendly 401 error
问题描述
我已经实现误差ASP.NET MVC网站<一个处理href=\"http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/620559#620559.\">in喜欢的方式表明,这种帖子。
I have implemented errors handling in ASP.NET MVC site in a way like suggests this post.
使用404错误一切工作正常。但如何正确显示用户友好的屏幕为 401错误?他们通常不会抛出异常,可以在的Application_Error()
而是动作返回HttpUnauthorizedResult处理。一种可能的方式是以下code添加到年底 Application_EndRequest()
法
With 404 errors all works fine. But how correctly show user friendly screen for a 401 error? They usually do not throw Exception that can be handled inside Application_Error()
but rather action returns HttpUnauthorizedResult. One possible way is to add following code to the end of Application_EndRequest()
method
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。
But inside Application_EndRequest()
Context.Session == null, errorController.Execute()
fails because it cannot use default 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.
所以,你可以建议一些最佳实践如何人性化处理401在ASP.NET MVC应用程序?
So, can you suggest some best practices how to 'user friendly handle' 401 in ASP.NET MVC application?
感谢。
推荐答案
看HandleErrorAttribute。从它的子类或添加您自己的实现,将处理所有的状态codeS你感兴趣,你可以把它返回的每个错误类型的单独的错误看法。
Look at the HandleErrorAttribute. Subclass from it or add your own implementation which will handle all the status codes you're interested in. You can make it to return a separate error view for each error type.
下面是如何创建处理错误异常过滤器的想法。我已经抛出了大部分的东西,只专注于我们的必需品。绝对看一下原始的实现添加参数检查和其他重要的事情。
Here is an idea of how to create your handle error exception filter. I've thrown out most of the stuff to only focus on our essentials. Absolutely have a look at the original implementation to add arguments checks and other important things.
public class HandleManyErrorsAttribute : FilterAttribute, IExceptionFilter
{
public virtual void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
return;
Exception exception = filterContext.Exception;
string viewName = string.Empty;
object viewModel = null;
int httpCode = new HttpException(null, exception).GetHttpCode();
if (httpCode == 500)
{
viewName = "Error500View";
viewModel = new Error500Model();
}
else if (httpCode == 404)
{
viewName = "Error404View";
viewModel = new Error404Model();
}
else if (httpCode == 401)
{
viewName = "Error401View";
viewModel = new Error401Model();
}
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];
filterContext.Result = new ViewResult
{
ViewName = viewName,
MasterName = Master,
ViewData = viewModel,
TempData = filterContext.Controller.TempData
};
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = httpCode;
filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
}
}
然后你来装饰与此属性你的控制器动作:
Then you "decorate" your controller actions with this attribute:
[HandleManyErrors]
public ActionResult DoSomethingBuggy ()
{
// ...
}
这篇关于ASP.NET MVC用户友好的401错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!