using System; using System.Collections.Generic; using System.Text; using System.IO; namespace Mtxfw.Utility { /// /// 自定义异常显示 by Telesa 2011.04.06 /// public class Error { private string _path = System.Web.HttpContext.Current.Server.MapPath("ErrorTemplate.htm"); private string isError; private Exception _exc; public Error(Exception ex) { //读取Web.Config的设置 true 则开启 isError = System.Configuration.ConfigurationManager.AppSettings["Error"]; _exc = ex; } /// /// 读取自定义异常的页面模板 /// /// private string Reader() { string str = string.Empty; try { if (File.Exists(_path)) { StreamReader sr = new StreamReader(_path, Encoding.GetEncoding("gb2312")); str = sr.ReadToEnd(); sr.Close(); sr.Dispose(); } } catch (Exception ex) { str = ex.Message; } return str; } /// /// 将异常信息写入到自定义页面 /// public void Write() { if (isError.ToLower() == "true") { string str = Reader(); if (str!=string.Empty) { str = str.Replace("$Message$", _exc.Message); System.Web.HttpContext.Current.Response.Write(str); System.Web.HttpContext.Current.Response.End(); System.Web.HttpContext.Current.Server.ClearError(); } } } } }