ASP.NET mvc異常處理的方法示例介紹
更新時間:2014年04月21日 12:00:21 作者:
這篇文章主要介紹了ASP.NET mvc異常處理的方法,需要的朋友可以參考下
1.首先常見保存異常的類(就是將異常信息寫入到文件中去)
public class LogManager
{
private string logFilePath = string.Empty;
public LogManager(string logFilePath)
{
this.logFilePath = logFilePath;
FileInfo file = new FileInfo(logFilePath);
if (!file.Exists)
{
file.Create().Close();
}
}
public void SaveLog(string message, DateTime writerTime)
{
string log = writerTime.ToString() + ":" + message;
StreamWriter sw = new StreamWriter(logFilePath, true);
sw.WriteLine(log);
sw.Close();
}
}
2、控制器異常處理
這種方式就在需要進(jìn)行異常處理的controller中重寫OnException()方法即可,因為它本身繼承了IExceptionFilter接口
public class ExceptionController : Controller
{
public ActionResult Index()
{
throw new Exception("我拋出異常了!");
}
protected override void OnException(ExceptionContext filterContext)
{
string filePath = Server.MapPath("~/Exception。txt");
StreamWriter sw = System.IO.File.AppendText(filePath);
sw.WriteLine(DateTime.Now.ToString() + ":" + filterContext.Exception.Message);
sw.Close();
base.OnException(filterContext);
Redirect("/");
}
}
3、過濾器異常處理
namespace MyMVC.Controllers
{
public class ExceptionController : Controller
{
[Error]
public ActionResult Index()
{
throw new Exception("過濾器異常!");
}
}
}
public class ErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
string path = filterContext.HttpContext.Server.MapPath("~/Exception.txt");
StreamWriter sw = System.IO.File.AppendText(path);
sw.WriteLine(DateTime.Now.ToString()+":"+filterContext.Exception.Message);
sw.Close();
}
}
復(fù)制代碼 代碼如下:
public class LogManager
{
private string logFilePath = string.Empty;
public LogManager(string logFilePath)
{
this.logFilePath = logFilePath;
FileInfo file = new FileInfo(logFilePath);
if (!file.Exists)
{
file.Create().Close();
}
}
public void SaveLog(string message, DateTime writerTime)
{
string log = writerTime.ToString() + ":" + message;
StreamWriter sw = new StreamWriter(logFilePath, true);
sw.WriteLine(log);
sw.Close();
}
}
2、控制器異常處理
這種方式就在需要進(jìn)行異常處理的controller中重寫OnException()方法即可,因為它本身繼承了IExceptionFilter接口
復(fù)制代碼 代碼如下:
public class ExceptionController : Controller
{
public ActionResult Index()
{
throw new Exception("我拋出異常了!");
}
protected override void OnException(ExceptionContext filterContext)
{
string filePath = Server.MapPath("~/Exception。txt");
StreamWriter sw = System.IO.File.AppendText(filePath);
sw.WriteLine(DateTime.Now.ToString() + ":" + filterContext.Exception.Message);
sw.Close();
base.OnException(filterContext);
Redirect("/");
}
}
3、過濾器異常處理
復(fù)制代碼 代碼如下:
namespace MyMVC.Controllers
{
public class ExceptionController : Controller
{
[Error]
public ActionResult Index()
{
throw new Exception("過濾器異常!");
}
}
}
public class ErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
base.OnException(filterContext);
string path = filterContext.HttpContext.Server.MapPath("~/Exception.txt");
StreamWriter sw = System.IO.File.AppendText(path);
sw.WriteLine(DateTime.Now.ToString()+":"+filterContext.Exception.Message);
sw.Close();
}
}
您可能感興趣的文章:
相關(guān)文章
ASP.NET學(xué)習(xí)CORE中使用Cookie身份認(rèn)證方法
本篇文章主要給大家詳細(xì)分析了ASP.NET學(xué)習(xí)CORE中使用Cookie身份認(rèn)證方法以及相關(guān)的實例代碼,有需要的朋友參考下吧。2018-01-01常用的在數(shù)據(jù)庫中建立無限級樹形菜單的asp.net代碼
經(jīng)常在項目中遇到建立無限級樹形菜單展示的效果,這里簡單地做了一個,基本后臺代碼如下2008-09-09.NET IoC模式依賴反轉(zhuǎn)(DIP)、控制反轉(zhuǎn)(Ioc)、依賴注入(DI)
這篇文章主要介紹了.NET IoC模式依賴反轉(zhuǎn)(DIP)、控制反轉(zhuǎn)(Ioc)、依賴注入(DI),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06log4net創(chuàng)建系統(tǒng)日志的詳細(xì)步驟
log4net是.Net下一個非常優(yōu)秀的開源日志記錄組件。log4net記錄日志的功能非常強(qiáng)大。它可以將日志分不同的等級,以不同的格式,輸出到不同的媒介。本文主要是簡單的介紹如何在Visual Studio2010(Asp.Net Mvc3.0)中使用log4net快速創(chuàng)建系統(tǒng)日志,如何擴(kuò)展以輸出自定義字段2013-11-11AjaxControlToolKit 顯示瀏覽者本地語言的方法
使用最新版的AjaxControlToolKit控件2008-12-12MVC+EasyUI+三層架構(gòu)簡單權(quán)限管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層架構(gòu)簡單權(quán)限管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07Entity Framework Core實現(xiàn)軟刪除與查詢過濾器
這篇文章介紹了Entity Framework Core實現(xiàn)軟刪除與查詢過濾器的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02ASP.NET通過更改Url進(jìn)行頁面?zhèn)髦档膶崿F(xiàn)代碼
這篇文章主要介紹了ASP.NET通過更改Url進(jìn)行頁面?zhèn)髦档膶崿F(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04