ASP.NET中最保险最环保的返回404的方法

代码如下:

Response.StatusCode = 404;
Response.SuppressContent = true;
Context.ApplicationInstance.CompleteRequest(); 

1. 只返回404 Status Code,自定义404页面在IIS的Error Pages中配置。

<httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">            
    <error statusCode="404" prefixLanguageFilePath="" path="D:\404.htm" responseMode="File" />            
</httpErrors>

2. 设置Response.SuppressContent为true,以防有响应内容被发给客户端。在我们的实际测试中发现,如果不设置Response.SuppressContent,在localhost中访问时,会看到部分响应内容。

3. 以前喜欢用霸道的Response.End()强制中止当前线程,而用Context.ApplicationInstance.CompleteRequest()更温和,也不会引发异常。

posted @ 2014-06-23 17:29  dudu  阅读(3545)  评论(0编辑  收藏  举报