一个ASP.NET的初级面试题

问题:

如果在一个B/S项目代码里没有执行try……catch,但又需要每次发生异常都跳转到一个友好的提示页面,而该项目中有上百个页面,不可能每个页面都再去加try……catch,请问你如何解决?


解决方案:

1、在Global.asax捕获整个解决方案中的异常错误

protected void Application_Error(object sender, EventArgs e)   
{
try
{
Server.Transfer(
"Error.aspx");
}
catch { }
}

Error.aspx:
Exception ex
= Server.GetLastError().GetBaseException();
if (ex != null)
{
Response.Write(ex.Message);
}
Server.ClearError();

2、通过webconfig.xml的错误配置

<configuration>

<system.web>

<customErrors mode="On" defaultRedirect="Error.aspx"/>

</system.web>

</configuration>

  

posted @ 2011-07-19 19:20  _Cafebabe  阅读(416)  评论(0编辑  收藏  举报