我自定义了一个ExceptionFilter 用于DoRelease这个Action,但结果是异常老是未捕获跳不到错误页面,请问是什么问题呢?本人是刚刚接触ASP.NET MVC模式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
namespace MVCDemo.Controllers.Filters
{
public class ExceptionFilter : FilterAttribute,IExceptionFilter
{
void IExceptionFilter.OnException(ExceptionContext filterContext)
{
filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
filterContext.Result = new ViewResult()
{
ViewName = "Error",
ViewData = filterContext.Controller.ViewData,
};
filterContext.ExceptionHandled = true;
}
}
}
[ExceptionFilter()]
public ActionResult DoRelease()
{
AnnounceInfo announce = new AnnounceInfo()
{
ID = 1,
Title = Request.Form["Title"],
Category = Int32.Parse(Request.Form["Category"]),
Content = Request.Form["Content"],
};
IAnnounceService aServ = ServiceBuilder.BuildAnnounceService();
aServ.Release(announce); //该条语句发生异常
ViewData["Announce"] = announce;
System.Threading.Thread.Sleep(2000);
ViewData["Time"] = DateTime.Now;
System.Threading.Thread.Sleep(2000);
return View("ReleaseSucceed");
}
给要拦截异常信息的Controller加[ExceptionFilte]特性声明