Why do I see 2 error pages when Server.Transfer is executed from Application_Error and errorMode = "Custom"?

My question is closely related to this question .

Here's a quick overview: My application runs in classic mode. I have the following code in Global.asax

    protected void Application_Error(Object sender, EventArgs e)
    {
        // ... boring stuff...
        HttpContext.Current.Server.Transfer("~/MyErrorPage.aspx", true);
    }

Everything works fine (i.e., I see MyErrorPage.aspx) when an error occurs if

<httpErrors errorMode="Detailed" />

but when errorMode="Custom"(or the errorMode="DetailedLocalOnly"request was sent from the remote machine), I see the IIS user error page with my error page (MyErrorPage.aspx).

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <!-- The contents of the default 500 page as configured in IIS 
         Which for me is the default %SystemDrive%\inetpub\custerr\<LANGUAGE-TAG>\500.htm
    -->
</html> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<!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" > 
    <!-- The contents of MyErrorPage.aspx -->
</html>

If I delete the default 500 error page from the IIS error page section, I get the following output (note that I get "Page cannot display ..." instead of a custom 500-page page)

The page cannot be displayed because an internal server error has occurred.
<!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" > 
    <!-- The contents of MyErrorPage.aspx -->
</html>

, , IIS 500 , Server.Transfer.
MyErrorPage.aspx( ). ( ) (HttpContext.Current.Response.Clear()) Server.Transfer, . Server.ClearError() , .

, , "fix" errormode="Detailed", , ASP.Net - , IIS 404 , url myApp/DoesNotExist.html. [ , , .]

"" - , , , : , URL- , , , , . , F5, . .

- , ? - ?

.

Edit

, :

http://rapidshare.com/files/427244682/Err.zip

[, .]

, , setstatus, . BANG

+3
3

<httpErrors> :

existingResponse="PassThrough"

. PassThrough , errorMode="Detailed".

http://blogs.iis.net/ksingla/archive/2008/02/18/what-to-expect-from-iis7-custom-error-module.aspx

0

Response Response.TrySkipIisCustomErrors, , -, IIS . true, , , 404 IIS.

0

, web.config. Server.GetLastError(). defaultResponseMode = "ExecuteURL", URL- .

<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="~/ErrorPages/CustomErrorPage.aspx" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="~/ErrorPages/404.aspx"/>
      <error statusCode="500" redirect ="~/ErrorPages/500.aspx"/>
</customErrors>
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace" defaultPath="~/ErrorPages/CustomErrorPage.aspx" defaultResponseMode="ExecuteURL">
      <error statusCode="404" path="~/ErrorPages/404.aspx"/>
      <error statusCode="500" path ="~/ErrorPages/500.aspx"/>
    </httpErrors>
</system.webServer>

CustomErrorPage Page_Load

    Exception ex = Server.GetLastError();
    if (ex is HttpException){
        Response.StatusCode = ((HttpException)ex).GetHttpCode();
    }

Thus, you have eliminated the need for Server.Transfer (this is done implicitly using enviromnet), and you have a common page for errors that are not handled explicitly.

0
source

Source: https://habr.com/ru/post/1770117/


All Articles