可能重复:
How can I catch a 404?
我想知道用c#asp.net捕获404错误的正确方法这里是我正在使用的代码
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(String.Format("http://www.gravatar.com/avatar/{0}?d=404", hashe));
// execute the request
try
{
//TODO: test for good connectivity first
//So it will not update the whole database with bad avatars
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Response.Write("has avatar");
}
catch (Exception ex)
{
if (ex.ToString().Contains("404"))
{
Response.Write("No avatar");
}
}
此代码有效,但我想知道这是否最有效。