ASP.NET MVC3 异常处理 摘抄
2012-11-19 16:41
127 查看
比较乱,参考文末链接
使用 handleError attribute 有以下局限:
1. Not support to log the exceptions 不支持exception记录
2. Doesn't catch HTTP exceptions other than 500 无法捕捉到500之外的http exception
3. Doesn't catch exceptions that are raised outside controllers controller之外抛出的异常无法处理
4. Returns error view even for exceptions raised in AJAX calls ajax调用出现exception时,会将错误页面内容返回
其他方法
1 重写Controller的OnException方法,其他继承自BaseController的Controller
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if(filtercontext==null)
return ;
var ex = filtercontext.exception??new exception("no further information exists");
logger.logerror(ex,"error general onexceipton");
filtercontext.exceptionhandled = true;
var data = {
errormessage = httputility.htmlencode(ex.message),
theexception = ex,
showmessage = !(filtercontext.exception==null),
showlink = false;
};
filtercontext.result = view("errorpage",data);
//base.OnException(filterContext);
}
}
2、创建FilterAttribute
通过FilterAttribute,并把它添加到全局过滤器集合中就可以提供给整个应用程序使用,如果只需要在某几个Action中使用,可以在Controller的Action中添加此特性。
Filter定义如下:
public class LogExceptionFilterAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// 添加记录日志代码
}
}
如果要应用给所有的Controller的所有Action,在Global.asax.cs中实现下面代码即可:
或者给只需要使用此特性的Controller或Action添加 :
3 使用 Elmah
4 global.asax 中添加如下事件
创建 ErrorController
参考
http://blog.163.com/elgyin@126/blog/static/189426202011101510232757/ http://www.prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc http://www.codeproject.com/Articles/422572/Exception-Handling-in-ASP-NET-MVC http://www.cnblogs.com/TomXu/archive/2011/12/15/2285432.html
使用 handleError attribute 有以下局限:
1. Not support to log the exceptions 不支持exception记录
2. Doesn't catch HTTP exceptions other than 500 无法捕捉到500之外的http exception
3. Doesn't catch exceptions that are raised outside controllers controller之外抛出的异常无法处理
4. Returns error view even for exceptions raised in AJAX calls ajax调用出现exception时,会将错误页面内容返回
其他方法
1 重写Controller的OnException方法,其他继承自BaseController的Controller
public class BaseController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
if(filtercontext==null)
return ;
var ex = filtercontext.exception??new exception("no further information exists");
logger.logerror(ex,"error general onexceipton");
filtercontext.exceptionhandled = true;
var data = {
errormessage = httputility.htmlencode(ex.message),
theexception = ex,
showmessage = !(filtercontext.exception==null),
showlink = false;
};
filtercontext.result = view("errorpage",data);
//base.OnException(filterContext);
}
}
2、创建FilterAttribute
通过FilterAttribute,并把它添加到全局过滤器集合中就可以提供给整个应用程序使用,如果只需要在某几个Action中使用,可以在Controller的Action中添加此特性。
Filter定义如下:
public class LogExceptionFilterAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// 添加记录日志代码
}
}
如果要应用给所有的Controller的所有Action,在Global.asax.cs中实现下面代码即可:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new LogExceptionFilterAttribute()); filters.Add(new HandleErrorAttribute()); }
或者给只需要使用此特性的Controller或Action添加 :
[LogExceptionFilter()] public ActionResult About() { throw new Exception("出错."); }
3 使用 Elmah
4 global.asax 中添加如下事件
protected void Application_Error() { var exception = Server.GetLastError(); var httpException = exception as HttpException; Response.Clear(); Server.ClearError(); var routeData = new RouteData(); routeData.Values["controller"] = "Errors"; routeData.Values["action"] = "General"; routeData.Values["exception"] = exception; //httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500; Response.StatusCode = 500; if (httpException != null) { Response.StatusCode = httpException.GetHttpCode(); switch (Response.StatusCode) { case 403: routeData.Values["action"] = "Http403"; break; case 404: routeData.Values["action"] = "Http404"; break; } } // Avoid IIS7 getting in the middle Response.TrySkipIisCustomErrors = true; IController errorsController = new ErrorsController(); HttpContextWrapper wrapper = new HttpContextWrapper(Context); var rc = new RequestContext(wrapper, routeData); errorsController.Execute(rc); }
创建 ErrorController
public class ErrorsController : Controller { public ActionResult General(Exception exception) { return View("Exception",exception); } public ActionResult Http404() { return View("404"); } public ActionResult Http403() { return View("403"); } }
参考
http://blog.163.com/elgyin@126/blog/static/189426202011101510232757/ http://www.prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc http://www.codeproject.com/Articles/422572/Exception-Handling-in-ASP-NET-MVC http://www.cnblogs.com/TomXu/archive/2011/12/15/2285432.html
相关文章推荐
- ASP.NET MVC3 异常处理
- ASP.NET MVC3 异常处理
- asp.net mvc3 异常处理
- 【转载】在ASP.NET MVC3 中利用JSONP跨域登录WEB系统
- 小议C#错误调试和异常处理
- C++ 容器类简介(摘抄)
- asp.net Mvc3 之 Html.BeginForm
- asp.net mvc3 css/js压缩合并---持续更新
- 异常处理
- ASP.NET MVC3中通过Request.IsAjaxRequest()来判断是否要加载公共视图
- 非预定义的异常处理
- 字符串匹配 wchar To char,摘抄别人的,自己改改,完成了自己的匹配。
- PHP的异常处理
- Struts 学习笔记2(输入校验、国际化、异常处理)
- python学习--文件、标准库、异常处理
- 第十七章 结合Service层讲解DAO层的异常处理
- PHP3 设计模式、COMPOSER、异常处理
- 不要递归的树形数组解析(摘抄)