0

如何404 error在 C# 中手动处理?

我想检查一些条件,404 error然后将其重定向到正确的页面。

我知道web.config设置,但是您不能检查web.config文件中的条件,可以吗?

4

2 回答 2

4

使用 HttpStatusCode 枚举,特别是HttpStatusCode.NotFound

就像是:

WebException we;
HttpWebResponse errorResponse = (HttpWebResponse)we.Response;
if (errorResponse.StatusCode == HttpStatusCode.NotFound)
{
    //
}

参考: 如何捕获 404?

类似的问题:

于 2012-06-16T07:22:04.160 回答
1

我自己找到了解决方案:

var sr = Server.GetLastError() as HttpException;
if (sr.GetHttpCode() == 404)
    ...
于 2012-06-16T07:36:24.393 回答