asp.net-mvc – ASP.NET MVC – 出错时的声明性重定向
发布时间:2020-12-16 06:30:29 所属栏目:asp.Net 来源:网络整理
导读:我一直在捕获异常并将用户重定向到错误页面.我传递了异常消息和返回URL,告诉用户发生了什么,并允许他们返回另一个页面. try { return action(parameters); } catch (Exception exception) { ErrorViewModel errorModel = new ErrorViewModel(); errorModel.E
我一直在捕获异常并将用户重定向到错误页面.我传递了异常消息和返回URL,告诉用户发生了什么,并允许他们返回另一个页面.
try { return action(parameters); } catch (Exception exception) { ErrorViewModel errorModel = new ErrorViewModel(); errorModel.ErrorMessage = "An error occured while doing something."; errorModel.ErrorDetails = exception.Message; errorModel.ReturnUrl = Url.Action("Controller","Action"); return RedirectToAction("Index","Error",errorModel); } 这似乎是太多的代码来包装每个动作.我正在使用全局过滤器来处理错误: public static void RegisterGlobalFilters(GlobalFilterCollection filters) { HandleErrorAttribute attribute = new HandleErrorAttribute(); filters.Add(attribute); } 我有我的web.config设置如下: <customErrors mode="On" defaultRedirect="~/Error/Unknown"> 但是,这仅适用于未处理的异常. 我希望异常导致重定向到错误控制器/操作,并获取包含异常详细信息的参数.如果我可以逐个动作地指示返回URL,或者如果没有提供默认值,则会很好. 解决方法
这是我创建的一个小的RedirectOnErrorAttribute类:
using System; using System.Web.Mvc; using MyApp.Web.Models; namespace MyApp.Web.Utilities { public class RedirectOnErrorAttribute : ActionFilterAttribute { /// <summary> /// Initializes a new instance of a RedirectOnErrorAttribute. /// </summary> public RedirectOnErrorAttribute() { ErrorMessage = "An error occurred while processing your request."; } /// <summary> /// Gets or sets the controller to redirect to. /// </summary> public string ReturnController { get; set; } /// <summary> /// Gets or sets the action to redirect to. /// </summary> public string ReturnAction { get; set; } /// <summary> /// Gets or sets the error message. /// </summary> public string ErrorMessage { get; set; } /// <summary> /// Redirects the user to an error screen if an exception is thrown. /// </summary> /// <param name="filterContext">The filter context.</param> public override void OnActionExecuted(ActionExecutedContext filterContext) { if (filterContext.Exception != null && !filterContext.ExceptionHandled) { ErrorViewModel viewModel = new ErrorViewModel(); viewModel.ErrorMessage = ErrorMessage; viewModel.ErrorDetails = filterContext.Exception.Message; string controller = ReturnController; string action = ReturnAction; if (String.IsNullOrWhiteSpace(controller)) { controller = (string)filterContext.RequestContext.RouteData.Values["controller"]; } if (String.IsNullOrWhiteSpace(action)) { action = "Index"; } UrlHelper helper = new UrlHelper(filterContext.RequestContext); viewModel.ReturnUrl = helper.Action(action,controller); string url = helper.Action("Index",viewModel); filterContext.Result = new RedirectResult(url); filterContext.ExceptionHandled = true; } base.OnActionExecuted(filterContext); } } } 现在,我可以像这样装饰我的所有动作: [RedirectOnError(ErrorMessage="An error occurred while doing something.",ReturnController="Controller",ReturnAction="Action")] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 什么是ASP.Net MVC查看引擎?
- entity-framework – 使用Entity框架核心生成和访问存储过程
- ASP.NET MVC 3可以在ASP.NET 3.5网站中运行吗?
- asp.net-mvc-2 – 在ASP.NET MVC2中向客户端返回http 204“
- ASP.NET MVC3视图是否有任何模板集合(HTML CSS)?
- asp.net-mvc – 在MVC3中下载Azure Blob文件
- asp.net-web-api – 从asp.net web api定制odata输出
- 在ASP.Net MVC中访问“应用程序”对象来存储应用程序范围的
- asp.net-mvc – ASP.MVC 2.0当整数值为零时,如何显示整数的
- 保持ASP.Net Session活跃的最佳方法
推荐文章
站长推荐
- 使用.NET 4.5的ASP.Net成员身份和实体框架迁移的
- asp.net – 对象不是垃圾回收,但不包含gcroot
- 如何在ASP.NET按钮中使用Twitter Bootstrap图标?
- asp.net – 为什么Request.QueryString只读?
- ASP.Net MVC授权操作过滤器
- asp.net-mvc – 使用自定义ASP.NET MVC IValuePr
- xamarin – ASP.NET核心Web API与ASP.NET核心应用
- asp.net-mvc-4 – 如何在mvc布局中添加徽标?
- [亲测]七步学会ASP.NET Core 2.0怎么发布/部署到
- asp.net – 错误:类型存在于两个目录中
热点阅读