在.NET处理404的更好 [英] Handling 404's better in .NET

查看:103
本文介绍了在.NET处理404的更好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于一些你们都在运行一些pretty大网站我想获得处理404的你的意见。我曾尝试不同的设置负荷与.net但从来没有真正得到它做正确的事情。我总是似乎得到某种重定向或假报头回来,从来没有一个404这是一个真正的痛苦,因为搜索引擎从来没有得到正确的反馈,但仍创下这些页面,尽管他们不再存在。反过来,这意味着我得到报告,因为我知道不再存在的页面错误。我也糊涂$对一些请求由净处理方式p $ ptty,有些是由IIS处理。

As some of you guys are running some pretty big sites I wanted to get your opinion on handling 404’s. I have tried loads of different setups with .Net but can never actually get it to do the right thing. I always seem to get some kind of redirect or false header returned, never a 404. This is a real pain because search engines never get the correct feedback and are still hitting these pages despite them no longer existing. In turn this means I get errors reported for pages I know no longer exist. I’m also pretty confused over the way some requests are handled by .Net and some are handled by IIS.

我目前的设置如下:IIS7,ASP.Net 3.5应用程序。我有web.config中设置的自定义错误页部分来处理404的使用新的redirectMode =ResponseRewrite属性,转发到一个HTML错误页面。 IIS配置为处理404的,并将其转发到相同的HTML页面。 ELMAH配置通过电子邮件报告任何此类问题了。

My current setup is as follows: IIS7, ASP.Net 3.5 App. I have the custom error pages section of web.config setup to handle 404’s using the new redirectMode="ResponseRewrite" property, forwarding to a html error page. IIS is configured to handle 404’s and forward them to the same html page. Elmah is configured to report any such issues via email too.

现在,当我尝试以下地址 http://www.severnside.com/document.aspx(一个页不存在),达网络处理错误并示出了200的响应。显然,这应该是404。当我尝试 http://www.severnside.com/document 我得到正确的404,但错误是由IIS处理。是否有可能安装了.NET处理这个错误太让ELMAH可以拿起错误?

Now when I try the following address http://www.severnside.com/document.aspx (a page that doesn’t exist), .net handles the error and shows a 200 response. Obviously this should be a 404. When I try http://www.severnside.com/document I get a correct 404 but the error was handled by IIS. Is it possible to have .Net handle this error too so Elmah can pick up the error?

这将是巨大的,如果我能得到一个洞察别人用过的正确处理这种场景的设置。

It would be great if I could get an insight into the setups used by others to handle this sort of scenario correctly.

感谢

推荐答案

而不是使用静态的HTML页面,你可以使用动态页面,这将设置正确的状态code的:

Instead of using static html page you could use dynamic page which will set the correct status code:

404.aspx:

<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
    Response.StatusCode = 404;
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Not found
    </div>
    </form>
</body>

web.config中:

web.config:

<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm" redirectMode="ResponseRewrite">
  <error statusCode="404" redirect="404.aspx" />
</customErrors>

这篇关于在.NET处理404的更好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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