ASP.NET自定义404返回而不是404 200 OK未找到
-
19-08-2019 - |
题
想设置我的网站谷歌网站管理员工具后,我发现我自定义ASP.NET 404页不返回404个状态码。它显示正确的自定义页面,并告诉浏览器一切正常。这是考虑软404或虚假的404.谷歌不喜欢这一点。所以我发现在这个问题上的文章很多,但我想解决方案似乎并没有工作。
我要工作在下面的两行增加背后的自定义404页的Page_Load方法的代码的溶液。
Response.Status = "404 Not Found";
Response.StatusCode = 404;
此不起作用。该页面仍返回200确定。然而,我发现,如果我硬代码下面的代码到设计代码,它会正常工作。
<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMaster" runat="server">
<%
Response.Status = "404 Not Found";
Response.StatusCode = 404;
%>
... Much more code ...
</asp:content>
在网页使用一个母版页。而我在配置我的web.config自定义错误页。我真的宁愿用后面的选项的代码,但我似乎无法让它不把一个黑客内嵌代码的设计/布局工作。
解决方案
<强>解决方案:强>
的问题,原来,是使用主页的。我把它通过在页面生命周期后设置状态代码工作,母版页明显呈现被重置,所以我推翻渲染方法和之后的渲染设置完成。
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
Response.StatusCode = 404;
}
更多的工作可以做,以找出究竟当母版页设置状态,但我会留给你。
<强>原始发表:强>
我能得到一个测试web应用程序正常工作,那么它至少显示自定义错误页,并返回404个状态码。我不能告诉你什么是错了你的应用程序,但我可以告诉你,我做了什么:
1)编辑在web.config定制错误:
<customErrors mode="On"> <error statusCode="404" redirect="404.aspx"/> </customErrors>
2)增加了一个404.aspx页面和状态代码设置为404。
public partial class _04 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.StatusCode = 404; } }
这就是这件事,如果我去由Asp.Net处理,不存在任何页面的扩展,我拉琴日志清楚地显示出一个404,这里是头部:
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
现在如果我去未被Asp.Net处理,就像一个htm文件的网页时,自定义页面不显示,并显示由IIS配置的404。
下面是进入一些细节,可能是使用的为您和您的问题后,我的测试确实做了重定向到一个新页面,以便请求的文件的URL差不多失去了(除了其在查询字符串)。
标题间谍响应:强>
HTTP/1.1 404 Not Found
Date: Sun, 07 Dec 2008 06:21:20 GMT
其他提示
我也有类似的问题,我想显示自定义页面为404(这是ASPX),它在本地主机上运行良好,但只要远程连接访问者他们将得到通用的IIS 404。
此的解决方案是添加
Response.TrySkipIisCustomErrors = true;
在改变Response.StatusCode。
在IIS 7解决方案是只添加到你的web.config文件:
<system.webServer>
<httpErrors existingResponse="Replace">
<remove statusCode="500" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="404.htm" responseMode="File" />
<error statusCode="500" prefixLanguageFilePath="" path="500.htm" responseMode="File" />
</httpErrors>
</system.webServer>
尝试调用到Response.End()跳过渲染...
Response.Status = "404 Not Found";
Response.StatusCode = 404;
Response.End();
return;
在许多测试和故障排除它似乎某些托管服务提供商可与返回代码干扰。我能够通过应用“黑客”中的内容来解决这个问题。
<%
// This code is required for host that do special 404 handling...
Response.Status = "404 Not Found";
Response.StatusCode = 404;
%>
这将允许网页无论什么返回正确的返回代码。
我能够用在使用.net 3.5 asp.net web表单下面的设置来解决这个问题。
我已经实现了模式绕过在web.config .NET的自定义重定向解决方案,我写我自己来处理所有的场景头中具有正确的HTTP状态代码。
首先,在web.config的的customErrors部分看起来像这样:
<customErrors mode="RemoteOnly" defaultRedirect="~/error.htm" />
该设置可以确保的customErrors模式设置为上,设置我们将在以后需要,并提供了一个所有其他人,失败的error.htm的defaultRedirect中选择。这会派上用场的时候我没有特定的错误处理程序,或有沿着破碎的数据库连接线的东西。
二,这里是全球ASAX错误事件:
protected void Application_Error(object sender, EventArgs e)
{
HandleError();
}
private void HandleError()
{
var exception = Server.GetLastError();
if (exception == null) return;
var baseException = exception.GetBaseException();
bool errorHandled = _applicationErrorHandler.HandleError(baseException);
if (!errorHandled) return;
var lastError = Server.GetLastError();
if (null != lastError && HttpContext.Current.IsCustomErrorEnabled)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(lastError.GetBaseException());
Server.ClearError();
}
}
此代码被假冒的处理错误到另一个类的责任。如果不处理的错误,的customErrors被打开,这意味着我们已经有了,我们是生产和莫名其妙的错误没有被处理的情况。我们会在这里清除,以防止用户看到它,但它登录在ELMAH所以我们知道发生了什么事情。
在applicationErrorHandler类看起来像这样:
public bool HandleError(Exception exception)
{
if (exception == null) return false;
var baseException = exception.GetBaseException();
Elmah.ErrorSignal.FromCurrentContext().Raise(baseException);
if (!HttpContext.Current.IsCustomErrorEnabled) return false;
try
{
var behavior = _responseBehaviorFactory.GetBehavior(exception);
if (behavior != null)
{
behavior.ExecuteRedirect();
return true;
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return false;
}
此类基本上使用命令图案来定位该核发错误的类型相应的错误处理程序。使用Exception.GetBaseException()在这个水平,因为几乎每一个错误都会在一个更高级别的异常被包装是很重要的。例如,在做“抛出新的System.Exception()”从任何aspx页将导致HttpUnhandledException在这一级被接收,而不是一个System.Exception的。
“工厂”的代码是简单且看起来像这样:
public ResponseBehaviorFactory()
{
_behaviors = new Dictionary<Type, Func<IResponseBehavior>>
{
{typeof(StoreException), () => new Found302StoreResponseBehavior()},
{typeof(HttpUnhandledException), () => new HttpExceptionResponseBehavior()},
{typeof(HttpException), () => new HttpExceptionResponseBehavior()},
{typeof(Exception), () => new Found302DefaultResponseBehavior()}
};
}
public IResponseBehavior GetBehavior(Exception exception)
{
if (exception == null) throw new ArgumentNullException("exception");
Func<IResponseBehavior> behavior;
bool tryGetValue = _behaviors.TryGetValue(exception.GetType(), out behavior);
//default value here:
if (!tryGetValue)
_behaviors.TryGetValue(typeof(Exception), out behavior);
if (behavior == null)
Elmah.ErrorSignal.FromCurrentContext().Raise(
new Exception(
"Danger! No Behavior defined for this Exception, therefore the user might have received a yellow screen of death!",
exception));
return behavior();
}
在结束时,我有一个可扩展的错误处理方案的设置。中定义的“行为”中的每一个,我有错误的类型的自定义实现。例如,HTTP异常将被检查的状态码和适当处理。 404个状态码将需要的Server.Transfer代替Request.Redirect,与写入在标题中的适当的状态码一起。
希望这有助于。