Files
2026-02-07 15:48:27 +08:00

68 lines
1.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Mtxfw.Utility
{
/// <summary>
/// 自定义异常显示 by Telesa 2011.04.06
/// </summary>
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;
}
/// <summary>
/// 读取自定义异常的页面模板
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 将异常信息写入到自定义页面
/// </summary>
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();
}
}
}
}
}