如果 URL 存在,我有一些代码可以显示按钮:
try
{
string dashboardURL = Config.RootUrl + "/Dashboard/Default.aspx";
WebRequest req = WebRequest.Create(dashboardURL);
WebResponse response = req.GetResponse();
btnDashboard.Visible = true;
}
catch (Exception)
{
btnDashboard.Visible = false;
}
但是,在调试时,req.getResponse()
会导致Application_Error
火灾。我检查了这里捕获的异常,它是一个System.Net.WebException
. 我的理解是Application_Error
因为未处理的异常而被解雇。
如果我更改代码以强制异常如下:
try
{
string dashboardURL = Config.RootUrl + "/Dashboard/Default.aspx";
WebRequest req = WebRequest.Create(dashboardURL);
int j = 0;
int i = 1 / j;
WebResponse response = req.GetResponse();
btnDashboard.Visible = true;
}
catch (Exception)
{
btnDashboard.Visible = false;
}
thenApplication_Error
不会被解雇,这很好。即使处理了异常,处理GetResponse()
总是导致触发的错误是否有什么特别之处?Application_Error