Tag: onexception

MVC HttpRequestValidationExceptionexception

我正在编写一个MVC 5互联网应用程序,并对HttpRequestValidationExceptionexception有疑问。 我之前在我的控制器中的代码如下: protected override void OnException(ExceptionContext filterContext) { // Make use of the exception later this.Session[“ErrorException”] = filterContext.Exception; if (filterContext.Exception is HttpRequestValidationException) { TempData[“UITitle”] = “Validation”; TempData[“UIHeading”] = customErrorType; TempData[“UIMessage”] = filterContext.Exception.Message; TempData[“UIException”] = filterContext.Exception; filterContext.ExceptionHandled = true; } else { TempData[“UITitle”] = “Error”; TempData[“UIHeading”] = customErrorType; TempData[“UIMessage”] = filterContext.Exception.Message; TempData[“UIException”] = filterContext.Exception; } […]

ASP.NET MVC Controller.OnException未被调用

我有一个基本控制器类,我将覆盖Controller.OnException处理程序方法,以便为将inheritance此类的某些类型的控制器提供一般error handling(这些控制器将返回JSON结果)。 当控制器引发exception时,永远不会调用OnException方法。 有谁看到我做错了什么,还是有更好的方法来处理这种情况? 使用MVC 1.0 基类: public class APIController : Controller { protected override void OnException(ExceptionContext filterContext) { //This is never called filterContext.Result = new JsonResult(); base.OnException(filterContext); } } inheritance类: public class MyController : APIController { public AjaxResult ForcedException() { throw new SystemException(); } }