【发布时间】:2016-01-10 02:41:18
【问题描述】:
当我的一个控制器中引发异常时,我会在我的基类中使用 OnException 捕获它,并且我想将异常对象传递给我的 ErrorController 的索引操作以在视图中显示。
在下面的示例中,我使用的是 TempData,它最终在到达我的 ErrorController 之前被丢弃。
我知道 TempData 只会持续到下一个请求,但为什么它没有那么远?
我也愿意通过其他方式解决这个问题。
测试控制器
public class TestController : BaseController
{
public ActionResult Index()
{
throw new Exception("test");
}
}
基本控制器
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if (filterContext.ExceptionHandled)
return;
filterContext.ExceptionHandled = true;
// Redirect to a different controller than the one that threw the exception
filterContext.Result = RedirectToAction("Index", "Error");
filterContext.Controller.TempData["exception"] = filterContext.Exception;
}
}
【问题讨论】:
-
临时数据在重定向时重置
标签: c# asp.net-mvc