ASP.NET自定义404返回而不是404 200 OK未找到 [英] ASP.NET Custom 404 Returning 200 OK Instead of 404 Not Found

查看:237
本文介绍了ASP.NET自定义404返回而不是404 200 OK未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想设置我的网站谷歌网站管理员工具后,我发现我自定义ASP.NET 404页不返回404状态code。它显示正确的自定义页面,并告诉浏览器一切正常。这是考虑软404或虚假的404.谷歌不喜欢这一点。所以我发现在这个问题上的文章很多,但我想解决方案似乎并没有工作。

我要努力在以下两行加入到后面的自定义404页的Page_Load方法的code的解决方案。

  Response.Status =404未找​​到;
Response.Status code = 404;

这是行不通的。该页面仍返回200确定。然而,我发现,如果我硬code以下code到设计code它将正常工作。

 < ASP:内容ID =ContentMainContentPlaceHolderID =ContentPlaceHolderMaster=服务器><%
Response.Status =404未找​​到;
Response.Status code = 404;
%GT; ...更多code ...< / ASP:内容>

该页面使用母版页。而我在配置我的web.config的自定义错误页。我真的宁愿用后面的选项code,但我似乎无法使它没有把一个黑客直列code在设计/布局工作。


解决方案

解决方案:

问题,它横空出世,是使用母版页。我得到了它被后来的状态设置为code在页面生命周期中,母版页明显呈现被重置它的工作,所以我推翻渲染方法和之后的渲染完成设置。

 保护覆盖无效渲染(HtmlTextWriter的作家)
{
    base.Render(作家);
    Response.Status code = 404;
}

更多的工作可以做,以找出究竟当主页设置状态,但我会留给你。


原贴:

我能得到一个测试web应用程序工作正常,那么它至少显示自定义错误页,并返回404状态code。我不能告诉你什么是错了你的应用程序,但我可以告诉你,我做了什么:

1)编辑web.config中的自定义错误:


 <的customErrors模式=ON>
  <错误状态code =404重定向=404.aspx/>
< /&的customErrors GT;


2)增加了一个404.aspx页面和状态code设置为404。


 公共部分类_04:System.Web.UI.Page
{
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        Response.Status code = 404;
    }
}


这就是这件事,如果我去由Asp.Net处理,不存在任何页面的扩展,我的小提琴手日志清楚地显示出一个404,这里是头:


  HTTP / 1.1 404未找​​到
服务器:Microsoft-IIS / 5.1
日期:孙老师,2008年6点04分13秒格林尼治标准​​时间12月07日
的X已启动方式:ASP.NET
的X ASPNET-版本:2.0.50727
缓存控制:私人
内容类型:text / html的;字符集= UTF-8
内容长度:533


现在如果我去不是由Asp.Net处理,就像一个htm文件的页面时,自定义页面不显示,显示由IIS配置的404。

下面是进入一些细节,可能是有用的你和你的问题后,我的测试确实做了重定向到一个新页面,以便请求文件的URL为pretty多少失去了(除其在查询字符串)。

<一个href=\"http://stackoverflow.com/questions/152307/google-404-and-net-custom-error-pages\">http://stackoverflow.com/questions/152307/google-404-and-net-custom-error-pages

头间谍响应:

  HTTP / 1.1 404未找​​到
日期:孙老师,2008年6时21分二十秒格林尼治标准​​时间12月07日

After trying to setup my site for Google Webmaster Tools I found that my Custom ASP.NET 404 page was not returning the 404 status code. It displayed the correct custom page and told the browser that everything is OK. This is consider a soft 404 or false 404. Google doesn't like this. So I found many articles on the issue but the solution I want didn't seem to work.

The solution I want to work is adding the following two lines to the code behind Page_Load method of the custom 404 page.

Response.Status = "404 Not Found";
Response.StatusCode = 404;

This doesn't work. The page still returns 200 OK. I found however that if I hard code the following code into the design code it will work properly.

<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMaster" runat="server">

<%
	Response.Status = "404 Not Found";
	Response.StatusCode = 404;
%>

 ... Much more code ...

</asp:content>

The page is using a master page. And I am configuring custom error pages in my web.config. I would really rather use the code behind option but I can't seem to make it work without putting a the hack inline code in the design / layout.

解决方案

Solution:

The problem, it turned out, was the use of the master page. I got it to work by setting the status code later in the pages lifecycle, obviously the rendering of the master page was resetting it, so I overrode the render method and set it after the render was complete.

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);
    Response.StatusCode = 404;
}

More work could be done to find out exactly when the master page is setting the status, but I'll leave that to you.


Original Post:

I was able to get a test web app to work fine, well it at least displayed the custom error page and returned a 404 status code. I can't tell you what is wrong with your app, but I can tell you what I did:

1) Edited the web.config for custom errors:

<customErrors mode="On">
  <error statusCode="404" redirect="404.aspx"/>
</customErrors>

2) Added a 404.aspx page and set the status code to 404.

public partial class _04 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.StatusCode = 404;
    }
}

Thats about it, if I go to any page extension that is processed by Asp.Net and does not exist, my fiddler log clearly shows a 404, here is the header:

HTTP/1.1 404 Not Found
Server: Microsoft-IIS/5.1
Date: Sun, 07 Dec 2008 06:04:13 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 533

Now if I go to a page that is not processed by Asp.Net, like a htm file, the custom page does not show and the 404 that is configured by IIS is displayed.

Here is a post that goes into some more details that may be of use to you and your problem, my test does do a redirect to the new page so the url of the requested file is pretty much lost (except its in the query string).

http://stackoverflow.com/questions/152307/google-404-and-net-custom-error-pages

Header Spy Response:

HTTP/1.1 404 Not Found
Date: Sun, 07 Dec 2008 06:21:20 GMT

这篇关于ASP.NET自定义404返回而不是404 200 OK未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆