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、控制器異常處理
這種方式就在需要進行異常處理的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、控制器異常處理
這種方式就在需要進行異常處理的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配合easyui實現(xiàn)返回json數(shù)據(jù)實例
這篇文章主要介紹了Asp.net配合easyui實現(xiàn)返回json數(shù)據(jù)的方法,實例分析了Asp.net配合easyui返回json數(shù)據(jù)時出現(xiàn)的問題及解決方法,非常具有實用價值的技巧,需要的朋友可以參考下2014-12-12
ASP.NET Core設(shè)置URLs的方法匯總(完美解決.NET 6項目局域網(wǎng)IP地址遠程無法訪問的
近期在dotnet項目中遇到這樣的問題.net6 運行以后無法通過局域網(wǎng)IP地址遠程訪問,整理出解決問題的五種方式方法,感興趣的朋友一起看看吧2023-11-11
VS2005打開VS2008項目的2種方法(vs2005怎么打開2008)
vs2008支持.net3.5,而vs2005支持.net2.0,所以使用vs2005打開vs2008的項目,要確定你的項目是.net2.0的,下面介紹二種VS2005打開VS2008項目的方法2014-01-01
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server
ASP.NET2.0數(shù)據(jù)庫入門之SQL Server...2006-09-09

