首页 新闻 赞助 找找看

WebApi

0
悬赏园豆:20 [已解决问题] 解决于 2015-09-28 17:43

webapi中用于处理方法引发的异常类是哪个?  比如mvc中是 HandleErrorAttribute 类。

我是用Log4net做的日志处理。也是在网上查找的代码, 重写( ActionFilterAttribute类,所有操作筛选器特性的基类,就是所有的action).  现在做测试就是我直接在action中throw new Exception("error"); 直接就在方法中抛异常了。我希望是能像mvc中一样直接不在代码中抛异常,在mvc中HandleErrorAttribute 类就能拿到action中抛出的异常 .  要不用这个log4net也就没啥用了。 另外webapi中有没有用于处理方法引发的异常类呢?

public override void OnActionExecuting(HttpActionContext filterContext)
{
var message = new StringBuilder();
message.Append(string.Format("Executing controller {0}, action {1}",
filterContext.ActionDescriptor.ControllerDescriptor.ControllerName,
filterContext.ActionDescriptor.ActionName));
log.Info(message);
}
//对每个请求的error做处理,这个需要重写OnActionExecuted
public override void OnActionExecuted(HttpActionExecutedContext filterContext)
{
if (filterContext.Exception == null)
{
return;
}
else
{
Exception ex = filterContext.Exception;
log.Error(ex);
}
}

学习态度要端正的主页 学习态度要端正 | 初学一级 | 园豆:55
提问于:2015-09-28 00:57
< >
分享
最佳答案
0

public class WebApiExceptionFilter : System.Web.Http.Filters.ExceptionFilterAttribute{}

config.Filters.Add(new WebApiExceptionFilter());

收获园豆:20
jj91821 | 菜鸟二级 |园豆:461 | 2015-09-28 08:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册