ASP.NET mvc異常處理的方法示例介紹
更新時(shí)間: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()方法即可,因?yàn)樗旧砝^承了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()方法即可,因?yàn)樗旧砝^承了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();
}
}
您可能感興趣的文章:
- asp.net?core?MVC?全局過濾器之ExceptionFilter過濾器(1)
- Asp.net Mvc 身份驗(yàn)證、異常處理、權(quán)限驗(yàn)證(攔截器)實(shí)現(xiàn)代碼
- 詳解使用Spring MVC統(tǒng)一異常處理實(shí)戰(zhàn)
- springboot springmvc拋出全局異常的解決方法
- ASP.NET MVC異常處理模塊詳解
- MVC異常處理詳解
- ASP.NET MVC下基于異常處理的完整解決方案總結(jié)
- 基于SpringMVC的全局異常處理器介紹
- ASP.NET MVC中異常處理&自定義錯(cuò)誤頁詳析
- ASP.NET MVC中異常Exception攔截的深入理解
相關(guān)文章
Asp.net配合easyui實(shí)現(xiàn)返回json數(shù)據(jù)實(shí)例
這篇文章主要介紹了Asp.net配合easyui實(shí)現(xiàn)返回json數(shù)據(jù)的方法,實(shí)例分析了Asp.net配合easyui返回json數(shù)據(jù)時(shí)出現(xiàn)的問題及解決方法,非常具有實(shí)用價(jià)值的技巧,需要的朋友可以參考下2014-12-12
ASP.NET C#生成下拉列表樹實(shí)現(xiàn)代碼
下拉列表樹很方便且時(shí)尚的一個(gè)導(dǎo)航,貌似很多的朋友都想實(shí)現(xiàn)這樣一個(gè)列表樹,本文將滿足你們的設(shè)想,通過本文你們可以學(xué)到如何使用c#生成下拉列表樹,感興趣的你可不要錯(cuò)過了啊2013-02-02
ASP.NET Core設(shè)置URLs的方法匯總(完美解決.NET 6項(xiàng)目局域網(wǎng)IP地址遠(yuǎn)程無法訪問的
近期在dotnet項(xiàng)目中遇到這樣的問題.net6 運(yùn)行以后無法通過局域網(wǎng)IP地址遠(yuǎn)程訪問,整理出解決問題的五種方式方法,感興趣的朋友一起看看吧2023-11-11
VS2005打開VS2008項(xiàng)目的2種方法(vs2005怎么打開2008)
vs2008支持.net3.5,而vs2005支持.net2.0,所以使用vs2005打開vs2008的項(xiàng)目,要確定你的項(xiàng)目是.net2.0的,下面介紹二種VS2005打開VS2008項(xiàng)目的方法2014-01-01
WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)定時(shí)刷新UI界面功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server...2006-09-09

