我一直在捕获异常并将用户重定向到错误页面。我传递了异常消息和返回URL来告诉用户发生了什么,并允许他们返回另一页。ASP.NET MVC - 错误声明重定向
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,或者如果没有提供任何默认行为,那么会很好。
这帮助......因此,该死的多 – TheGeekZn 2014-08-27 11:36:01