C# ASP.Net-无法使自定义错误页正常工作
我有一个C# ASP.Net-无法使自定义错误页正常工作,c#,asp.net,C#,Asp.net,我有一个asp.net网站,我正试图配置404页面,但我似乎无法让它工作 我在web.config中的代码是 <system.web> <customErrors mode="On" defaultRedirect="ResponseRewrite"> <error statusCode="404" redirect="~/Page_Not_Found.aspx" /> </customErrors> </system.web
asp.net
网站,我正试图配置404页面,但我似乎无法让它工作
我在web.config中的代码是
<system.web>
<customErrors mode="On" defaultRedirect="ResponseRewrite">
<error statusCode="404" redirect="~/Page_Not_Found.aspx" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="~/Page_Not_Found.aspx" responseMode="Redirect"/>
</httpErrors>
</system.webServer>
错误页HTML
<%@ Page Title="- Page not found" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Page_Not_Found.aspx.cs" Inherits="WebApplication1.Page_Not_Found" %>
<asp:Content ID="404BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="col-xs-12 col-sm-9">
<h1>Page not found</h1>
<p>Use this area to provide additional information.</p>
<p>Use this area to provide additional information.</p>
<p>Use this area to provide additional information.</p>
<p>Use this area to provide additional information.</p>
<p>Use this area to provide additional information.</p>
<p>Use this area to provide additional information.</p>
</div>
</asp:Content>
找不到页面
使用此区域可提供其他信息
使用此区域可提供其他信息
使用此区域可提供其他信息
使用此区域可提供其他信息
使用此区域可提供其他信息
使用此区域可提供其他信息
当我输入无效的URL时,我得到下面的服务器错误
“/”应用程序中出现服务器错误。运行时错误描述:一个
处理您的请求时发生异常。另外,,
执行的自定义错误页时发生另一个异常
第一个例外。请求已被终止
我做错了什么
我已经在这里和web上查看了其他解决方案,但所有的修复似乎对我没有帮助。可能是引发了一个异常,而您的应用程序没有捕获到该异常(内部服务器错误) 尝试添加以下内容:
<error statusCode="500" redirect="https://www.google.fr/"/>
并检查它是否在谷歌上重定向你。
如果是,这是一个与应用程序代码相关的问题,而不是因为IIS
另外,我觉得defaultRedirect=“ResponseRewrite”
似乎不正确。
它不应该在redirectMode=“ResponseRewrite”
属性中吗
默认重定向是IIS重定向的页面,如果错误不是详细自定义错误的一部分(在您的示例中为404)
如果您的应用程序抛出500个错误代码,您将在ResponseWrite上被重定向,IIS可能找不到该错误代码。您需要做两件事 1) 关闭“自定义错误”以了解导致问题的真正原因 2) 在IIS8中,似乎在自定义错误页面的路径中使用“~”tilda存在问题。请尝试以下操作:
<system.web>
<customErrors mode="On" defaultRedirect="ResponseRewrite">
<error statusCode="404" redirect="/Page_Not_Found.aspx" />
</customErrors>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom">
<remove statusCode="404"/>
<error statusCode="404" path="/Page_Not_Found.aspx" responseMode="Redirect"/>
</httpErrors>
</system.webServer>
另外值得一提的是,如果您是IIS7+,请不要同时使用两个配置项,然后确保您使用httpErrors而不是customErrors
参考资料:
我怀疑要显示为404页面的页面会生成异常。这并不一定意味着页面本身存在异常,它也可能来自页面使用的母版页 您可以尝试在全局asax中设置断点:
protected void Application_Error()
{
var error = Server.GetLastError();
Logger.Log(error); //This is my own Logging library, so implement something yourself, but you get the idea.
}
当此代码触发时,您可以检查内部异常详细信息并查看发生了什么。最终发现了我的问题所在。这是我在
asp:Content
中遇到的ID
问题,我在其中添加了404(显示在我的初始帖子中),如果我删除它或将其更改为字母字符,它就可以工作
是
<asp:Content ID="404BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Content ID="FourOFourBodyContent" ContentPlaceHolderID="MainContent" runat="server">
修复-已删除
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
修复-字母字符
<asp:Content ID="404BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:Content ID="FourOFourBodyContent" ContentPlaceHolderID="MainContent" runat="server">
我想可能是服务器出错了。禁用自定义错误并检查实际的异常消息。@RahulSingh服务器错误中没有任何信息,因为我已经发布了我在第一篇文章中得到的信息。我在我的项目中没有任何其他自定义错误,因为我看起来我已经尝试了你的代码,但仍然遇到相同的问题,我没有重定向到Google。你能告诉我你的IIS版本是什么吗?如果我关闭自定义消息,我得到的服务器错误是>应用程序中的服务器错误。>找不到资源。>描述:HTTP404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下URL并确保其拼写正确。>请求的URL:/default1该页面是否真的丢失或不可用?该页面确实存在,就好像我键入了可以访问的URL一样it@murday1983如果它存在,你不应该得到404,对吗?可能值得检查您的IIS配置并确保您的应用程序设置正确。