您的位置:首页 > 其它

MVC继承Controller类并重写OnException方法实现全局错误日志

2015-02-05 13:58 537 查看
1)创建类BaseController.cs ,继承Controller类并重写OnException方法,代码如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication70.Controllers
{
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
// 错误日志编写
string controllerNamer = filterContext.RouteData.Values["controller"].ToString();
string actionName = filterContext.RouteData.Values["action"].ToString();
string exception = filterContext.Exception.ToString();
// 执行基类中的OnException
base.OnException(filterContext);
}
}
}
2)然后其他需要记录日志的Controller继承BaseController即可

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication70.Controllers
{
public class HomeController : BaseController
{
public ActionResult Index()
{
string test1 = "a";
int test2 = Int32.Parse(test1);
return View();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐