最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET mvc異常處理的方法示例介紹

 更新時間:2014年04月21日 12:00:21   作者:  
這篇文章主要介紹了ASP.NET mvc異常處理的方法,需要的朋友可以參考下
1.首先常見保存異常的類(就是將異常信息寫入到文件中去)
復(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)文章

最新評論

搜索| 大名县| 重庆市| 临海市| 桐柏县| 新兴县| 阿巴嘎旗| 汝阳县| 治多县| 喀喇沁旗| 襄汾县| 木兰县| 武强县| 手机| 彩票| 年辖:市辖区| 甘肃省| 栾城县| 泸州市| 峡江县| 苗栗市| 藁城市| 惠来县| 老河口市| 金堂县| 东港市| 平陆县| 中江县| 吉林市| 习水县| 抚松县| 普宁市| 奇台县| 霍邱县| 石棉县| 永济市| 定南县| 盐源县| 襄垣县| 浦县| 木兰县|