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();
}
}
}
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();
}
}
}
相关文章推荐
- MVC继承Controller类并重写OnException方法实现全局错误日志
- 通过JQuery调用MVC的Controller方法来实现无刷新操作
- 一步一步重写 CodeIgniter 框架 (7) —— Controller执行时将 Model获得的数据传入View中,实现MVC
- C# 继承实现父类方法、重写、重载
- 线程 创建和启动线程的两种方式 实现Runnable接口 继承Thread类 重写唯一方法run()
- C#中继承实现父类方法、重写、重载
- 通过JQuery调用MVC的Controller方法来实现无刷新操作
- C#中继承实现父类方法、重写、重载
- java 子类继承父类成员变量的隐藏、实现方法的重写
- 通过JQuery调用MVC的Controller方法来实现无刷新操作
- -- Lua 实现类继承 方法重写
- Asp.net MVC中的全局权限验证方法及实现
- 实现Square类,让其继承自Rectangle类,并在Square类增添新属性和方法,在2的基础上,在Square类中重写Rectangle类中的初始化和打印方法
- ASP.NET实现MVC中获取当前URL、controller及action的方法
- asp.net继承page类重写方法 实现最基本的用户登录验证 权限验证等
- 用类的继承关系(重写父类的方法)实现简易后台代码模板
- 用java中继承和方法重写实现求最大公约数和最小公倍数
- Servlet继承HttpServlet重写service方法的实现!
- MVC用filter做权限过滤: 1:创建filter类;类需要继承一个接口,并且重写4个方法;如下: namespace MvcApplication1.Code { public clas
- (不推荐使用)springMVC基本配置+继承MultiActionController来实现根据参数名指定要请求的方法