asp.net-mvc – ASP.NET MVC用户友好401错误
发布时间:2020-05-24 01:44:34 所属栏目:asp.Net 来源:互联网
导读:我在ASP.NET MVC site in a way like suggests this post中实现了错误处理. 404错误都可以正常工作.但是如何正确显示用户友好的屏幕401错误?他们通常不会抛出可以在Application_Error()中处理的异常,而是返回HttpUnauthorizedResult.一种可能的方法是将以下
|
我在ASP.NET MVC site 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? 谢谢. 解决方法看看HandleErrorAttribute.从它的子类或添加您自己的实现,将处理您感兴趣的所有状态代码.您可以使它为每个错误类型返回单独的错误视图.这是一个如何创建你的句柄错误异常过滤器的想法.我抛出了大部分的东西,只关注我们的要领.绝对看一下原来的实现,添加参数检查和其他重要的事情. 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;
}
}
然后,使用此属性“装饰”控制器操作: [HandleManyErrors]
public ActionResult DoSomethingBuggy ()
{
// ...
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-web-api – 如何从ASP.net 5 web api返回文件
- asp.net – URL重写出站规则IIS7
- 实体框架 – 实体框架验证混淆 – 最大字符串长度为’128′
- asp.net-mvc – 如何在asp.net C#中从http重定向到https并将
- 在ASP.NET Web App中查找内存泄漏
- asp.net – 在MVC5应用程序中使用OWIN软件包的好处
- asp.net-mvc – 此模板试图加载组件程序集Microsoft.Visual
- asp.net – MS Chart for .NET预定义调色板颜色列表?
- asp.net – 对框架程序集“System.Runtime,Version = 4.0.1
- 在asp.net控件的style属性中使用DataBinder.Eval()
推荐文章
站长推荐
- asp.net – HttpPostedFileBase.SaveAs方法问题
- asp.net-mvc-5 – 依赖注入结构图ASP.NET Identi
- 如何不缓存ASP.NET用户控件?
- owin – 如何在Startup.cs中添加CamelCaseProper
- regex – RegularExpressionAttribute – 如何使
- asp.net – 允许服务器/ usercontrol上的任何属性
- asp.net – 存储CheckBoxList的DataValueField值
- asp.net-mvc-3 – jQuery Mobile/MVC:使用Redir
- 如何在服务器上安装ASP.NET MVC 5?
- .net – Razor RTM中的声明性助手方法
热点阅读
