- ·上一篇文章:ASP.NET底层架构探索之再谈.NET运行时(一)
- ·下一篇文章:ASP.NET 2.0中发送电子邮件剖析之一
错误处理
两种方式:
一、返回错误信息和错误码,这样客户端可以把错误信息直接显示给用户,省去了解析错误码的烦恼。
服务器端实现:
下面的类解析错误码定义文件,并且把错误信息加入hastTable
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.Caching;
using System.Collections;

namespace MeetingProxy.MeetingException


{

/**//// <summary>
/// 错误码的描述
/// </summary>
public class ErrProcedure

{

private static Hashtable errMessages = new Hashtable();
public static Hashtable GetErrMessages()

{
if (CommonCache.Get("ErrMessage") as Hashtable == null)

{
string path = null;

HttpContext context = HttpContext.Current;
if (context != null)
path = context.Server.MapPath("~ErrMessage.xml");
else
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrMessage.xml");

XmlDocument xdoc = new XmlDocument();
xdoc.Load(path);
foreach (XmlNode child in xdoc.LastChild)

{
errMessages.Add(int.Parse(child.LastChild.InnerText), child.FirstChild.InnerText);
}
CacheDependency cd = new CacheDependency(path);
CommonCache.Max("ErrMessage", errMessages,cd);
return errMessages;

}
else

{
return CommonCache.Get("ErrMessage") as Hashtable;
}
}



}
}
错误码文件:
<?xml version="1.0" encoding="UTF-8"?>
<ErrMessage>
<Err>
<Description>登录会议室错误</Description>
<ErrCode>100</ErrCode>
</Err>
<Err>
<Description>您申请的会议室被别人抢用,会议室创建失败,请重新申请</Description>
<ErrCode>101</ErrCode>
</Err>
<Err>
<Description>必须有端口号</Description>
<ErrCode>200</ErrCode>
</Err>
<Err>
<Description>用户名或者密码错误</Description>
<ErrCode>300</ErrCode>
</Err>
</ErrMessage>

异常定义:
using System;
using System.Collections.Generic;
using System.Text;

namespace MeetingProxy


{
public class MeetingBaseException:ApplicationException

{
int errCode;
public MeetingBaseException(string message, int errCode)
: base(message)

{
this.errCode = errCode;
}
public MeetingBaseException(int errCode)

{
this.errCode = errCode;
}
public int ErrCode

{

get
{ return errCode; }

set
{ this.errCode = value; }
}
}
}

异常抛出方式:
private string VerifySession()

{
string passport = null;
if (HttpContext.Current != null)

{


passport = HttpContext.Current.Session["Username"] as string;
}
if (passport == null)

{
//用户没有通过Session验证
throw new MeetingBaseException(ErrProcedure.GetErrMessages()[301].ToString(), 301);
}
return passport;
}客户端:
客户端如果检测到是MeetingBaseException就可以直接把错误信息显示给客户,而不用解析错误码。
缺点:虽然省去了错误码的解析,但是如果有多语言化问题,错误信息直接显示就不行了。
如果客户端想只显示某些错误信息给客户,这种方式也不方便。
一、返回错误信息和错误码,这样客户端可以把错误信息直接显示给用户,省去了解析错误码的烦恼。
服务器端实现:
下面的类解析错误码定义文件,并且把错误信息加入hastTable
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Web;
using System.Web.Caching;
using System.Collections;
namespace MeetingProxy.MeetingException

{
/**//// <summary>
/// 错误码的描述
/// </summary>
public class ErrProcedure
{
private static Hashtable errMessages = new Hashtable();
public static Hashtable GetErrMessages()
{
if (CommonCache.Get("ErrMessage") as Hashtable == null)
{
string path = null;
HttpContext context = HttpContext.Current;
if (context != null)
path = context.Server.MapPath("~ErrMessage.xml");
else
path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ErrMessage.xml");
XmlDocument xdoc = new XmlDocument();
xdoc.Load(path);
foreach (XmlNode child in xdoc.LastChild)
{
errMessages.Add(int.Parse(child.LastChild.InnerText), child.FirstChild.InnerText);
}
CacheDependency cd = new CacheDependency(path);
CommonCache.Max("ErrMessage", errMessages,cd);
return errMessages;
}
else
{
return CommonCache.Get("ErrMessage") as Hashtable;
}
}


}
}
<?xml version="1.0" encoding="UTF-8"?>
<ErrMessage>
<Err>
<Description>登录会议室错误</Description>
<ErrCode>100</ErrCode>
</Err>
<Err>
<Description>您申请的会议室被别人抢用,会议室创建失败,请重新申请</Description>
<ErrCode>101</ErrCode>
</Err>
<Err>
<Description>必须有端口号</Description>
<ErrCode>200</ErrCode>
</Err>
<Err>
<Description>用户名或者密码错误</Description>
<ErrCode>300</ErrCode>
</Err>
</ErrMessage>

using System;
using System.Collections.Generic;
using System.Text;
namespace MeetingProxy

{
public class MeetingBaseException:ApplicationException
{
int errCode;
public MeetingBaseException(string message, int errCode)
: base(message)
{
this.errCode = errCode;
}
public MeetingBaseException(int errCode)
{
this.errCode = errCode;
}
public int ErrCode
{
get
{ return errCode; }
set
{ this.errCode = value; }
}
}
}
异常抛出方式:
private string VerifySession()
{
string passport = null;
if (HttpContext.Current != null)
{

passport = HttpContext.Current.Session["Username"] as string;
}
if (passport == null)
{
//用户没有通过Session验证
throw new MeetingBaseException(ErrProcedure.GetErrMessages()[301].ToString(), 301);
}
return passport;
}客户端如果检测到是MeetingBaseException就可以直接把错误信息显示给客户,而不用解析错误码。
缺点:虽然省去了错误码的解析,但是如果有多语言化问题,错误信息直接显示就不行了。
如果客户端想只显示某些错误信息给客户,这种方式也不方便。
