首页 技术 正文
技术 2022年11月6日
0 收藏 877 点赞 578 浏览 3492 个字

[整理]ASP.NET 中异常处理

1.直接通过重写Controller的OnException来处理异常#

public class HomeController : Controller

{

public ActionResult Index( )

{

throw new Exception( "test ex." );

return View( );

}

protected ILog Log
{
get { return LogManager.CreateLog( ); }
}protected override void OnException( ExceptionContext filterContext )
{
// 处理异常
if( !filterContext.ExceptionHandled )
{
var ex = filterContext.Exception;
Log.Error( ex.Message );
Log.Error( ex.StackTrace ); filterContext.ExceptionHandled = true;
} base.OnException( filterContext );
}

}

2.通过添加自定义Filter并继承HandleErrorAttribute,重写OnException来处理异常

public class MvcHandleErrorAttribute : HandleErrorAttribute

{

protected ILog Log

{

get { return LogManager.CreateLog( ); }

}

public override void OnException( ExceptionContext filterContext )
{
// 处理异常
if( !filterContext.ExceptionHandled )
{
var ex = filterContext.Exception;
Log.Error( ex.Message );
Log.Error( ex.StackTrace ); filterContext.ExceptionHandled = true;
} base.OnException( filterContext );
}

}

public class FilterConfig

{

public static void RegisterGlobalFilters( GlobalFilterCollection filters )

{

https://filters.add/( new HandleErrorAttribute( ) );

filters.Add( new MvcHandleErrorAttribute( ) );

}

}

方案1和2可以单独使用,也可以同时使用,但是要注意的是,如果方案1和2同时存在,先进入方案2的OnException,后进入方案1的OnException,所以为了避免重复处理,在处理完异常后,执行

filterContext.ExceptionHandled = true;

这样,服务端响应返回一个EmptyResult实例,而不再返回一个异常(常见的黄页)

方案2中,由于自定义Filter是特性Attribute,即可以设置全局,也可以设置Controller,也可以设置Action,

3.通过自定义IHttpModule或是Global里处理异常

public class ExceptionModule : IHttpModule

{

private HttpApplication _application;

protected ILog Log
{
get { return LogManager.CreateLog( ); }
}public void Dispose( )
{
//DO NOTHING
}public void Init( HttpApplication context )
{
_application = context; //注册异常事件方法
_application.Error += new EventHandler( OnApplicationError );
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );
}void OnApplicationError( object sender, EventArgs e )
{
var loggedAccout = WebWorkContext.Current.LoggedAccount;
var ex = _application.Server.GetLastError( ); Log.Error( ( loggedAccout == null ) ? "n/a" : loggedAccout.Name );
Log.Error( ex.Message );
Log.Error( ex.StackTrace );
}void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e )
{
if( e.ExceptionObject == null )
return; Exception ex = e.ExceptionObject as Exception;
Log.Error( ex.Message );
Log.Error( ex.StackTrace );
}

}

和自定义IHttpModule里一样,在Global的 Application_Start里注册异常事件方法

public class MvcApplication : System.Web.HttpApplication

{

protected void Application_Start( )

{

//注册异常事件方法

this.Error += new EventHandler( OnApplicationError );

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );

}

void OnApplicationError ( object sender, EventArgs e )
{
var ex = _application.Server.GetLastError( );
Log.Error( ex.Message );
Log.Error( ex.StackTrace );
}

}

4.未解决的问题

public class HomeController :Controller

{

public ActionResult Index( )

{

int count = 0;

  Thread thread = new Thread( ( ) =>
{
throw new Exception( "Test线程异常" );//异常无限循环抛出,WebDev.Server4.0.exe停止工作。如何解决?
} ); thread.Start( ); //throw new Exception( "test ex." ); return View( );
}

}

5.WebApi中

public class WebApiExceptionFilterAttribute : ExceptionFilterAttribute

{

protected ILog Log

{

get { return LogManager.CreateLog( ); }

}

public override void OnException( HttpActionExecutedContext actionExecutedContext )
{
//处理异常
var ex = actionExecutedContext.Exception;
Log.Error( ex.Message );
Log.Error( ex.StackTrace ); base.OnException( actionExecutedContext );
}

}

public static class WebApiConfig

{

public static void Register( HttpConfiguration config )

{

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

}

}

参考:

http://www.cnblogs.com/luminji/archive/2011/01/05/1926033.html

http://www.asp.net/web-api/overview/error-handling/exception-handling

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,996
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,510
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,353
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,137
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848