简体   繁体   English

ASP.Net-无法获得我的自定义错误页面

[英]ASP.Net - Cant Get My Custom Error Page Working

I have an asp.net website and i am rying to configure a 404 page but i cant seem to get it working. 我有一个asp.net网站,我正在尝试配置404页面,但我似乎无法使其正常工作。

The code i have in my web.config is 我在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>

Error page HTML 错误页面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>

When I enter and invalid URL i get the below server error 当我输入无效的URL时,出现以下服务器错误

Server Error in '/' Application. “ /”应用程序中的服务器错误。 Runtime Error Description: An exception occurred while processing your request. 运行时错误说明:处理您的请求时发生异常。 Additionally, another exception occurred while executing the custom error page for the first exception. 此外,在执行第一个异常的自定义错误页面时发生另一个异常。 The request has been terminated. 该请求已终止。

What am I doing wrong? 我究竟做错了什么?

I have looked at other solutions on here and also on the web but all the fixes dont seem to help me. 我在这里和网络上都看过其他解决方案,但是所有修复似乎都没有帮助。

Might be an Exception thrown that is not caught in your application (internal server error) 可能是您的应用程序未捕获的抛出异常(内部服务器错误)

Try adding this : 尝试添加:

<error statusCode="500" redirect="https://www.google.fr/"/>

And check if it's redirecting you on Google. 并检查它是否在Google上重定向了您。 If so, this is an application code related issue and not because of the IIS. 如果是这样,这是与应用程序代码相关的问题,而不是因为IIS。

Also defaultRedirect="ResponseRewrite" seems incorrect to me. 另外defaultRedirect="ResponseRewrite"在我看来defaultRedirect="ResponseRewrite"正确。 Shouldn't it be in redirectMode="ResponseRewrite" attribute? 它不应该在redirectMode="ResponseRewrite"属性中吗?

Default redirect is the page that IIS is redirecting if error is not part of the detailed custom error (404 in your case). 如果错误不是详细的自定义错误(在您的情况下为404)的一部分,则默认重定向是IIS重定向的页面。

If your app throw an 500 error code, you'll get redirected on ResponseRewrite, which the IIS may not find 如果您的应用抛出500错误代码,您将在ResponseRewrite上重定向,IIS可能找不到该重定向

I suspect that the page you want to show as a 404 page generates an exception. 我怀疑您要显示为404页的页面会产生异常。 This doesn't necessarily mean the exception is in the page itself, it could also come from the Master page the page uses. 这并不一定意味着该页面本身就是例外,它也可能来自该页面使用的母版页。

You can try setting a breakpoint in your global asax: 您可以尝试在全局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.
}

When this code fires, you can examine the inner exception details and see what is happening. 触发该代码后,您可以检查内部异常详细信息并查看发生了什么。

There are two things you need to do. 您需要做两件事。

1) Turn off custom errors to get to the bottom of what's actually causing the issue. 1)关闭自定义错误,以深入了解导致问题的根本原因。

2) It seems in IIS8 there are issues with using '~' tilda in the path for custom error page please try the following: 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>

http://forums.asp.net/t/1938152.aspx?Tilde+character+not+recognized+in+IIS8+for+custom+error+pages+in+web+config http://forums.asp.net/t/1938152.aspx?Tilde+character+not+recognized+in+IIS8+for+custom+error+pages+in+web+config

Also its worth mentioning don't use both configuration entries if you are IIS7+ then ensure you use httpErrors over customErrors. 同样值得一提的是,如果您使用的是IIS7 +,则不要同时使用两个配置条目,请确保在customErrors上使用httpErrors。

References: 参考文献:

Asp.Net Forums - Tilda Not Recognised Asp.Net论坛-无法识别Tilda

Stackoverflow - Difference between httpErrors and customErrors Stackoverflow-httpErrors和customErrors之间的区别

Finally found out what my issue was. 终于发现我的问题是什么。 It was an issue i had with the ID in my asp:Content , I had added 404 to it (displayed in my inital post), if i removed it or changed it to alphacharacters it works 这是我在asp:ContentID所遇到的问题,如果我将其删除或将其更改为alphacharacters,则向其中添加了404(显示在我的初始帖子中)

Was

<asp:Content ID="404BodyContent" ContentPlaceHolderID="MainContent" runat="server">

Fix - Removed 修复-已删除

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

Fix - Alpha characters 修复-字母字符

<asp:Content ID="FourOFourBodyContent" ContentPlaceHolderID="MainContent" runat="server">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM