1
votes

One of our legacy websites based on the classic ASP.NET technology was moved to a new hosting. Now it's a cloud running Windows Azure Pack in which our site is running under .NET Framework 4.6.1.

However, I still can't return a custom error page for a non-existing .aspx resource with the correct HTTP status 404 - the status is always 302. Originally the custom 404 error page was defined like this in web.config:

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="/error/server-error.aspx">
        <error statusCode="404" redirect="/error/resource-not-found.aspx"/>
    </customErrors>
</system.web>

I did read SO posts like this and tried the suggested answers including setting the redirectMode to ResponseRewrite, but nothing helped or did not work as expected.

Did I miss something, or is there another solution?

3

3 Answers

1
votes

However, I still can't return a custom error page for a non-existing .aspx resource with the correct HTTP status 404 - the status is always 302.

As far as I known, HTTP 302 means that the requested resource has been moved to a different location. Based on your description, I created an ASP.NET Web Application and deployed it to Azure Web App with the custom error configuration you provided.

Here is my detailed test:

  1. Turn off the mode of CustomErrors

    If the resource you requested is not found, then you could get the response header as follows via Fiddler or Developer Tools in your browser.

  2. Set the mode of CustomErrors as RemoteOnly

    With the configuration you provided, when the requested resource is not found, you could get the details as follows:

Upon the test, I have tested successfully both on my side and the Azure Web App. Please try to check the location header when you get the 302 response via Fiddler. In additionally, if you hope that when the resource is not found and the browser would redirect to the resource-not-found.aspx page with 404 status code, you could follow this related thread.

1
votes

I've managed to overcome this problem. First, I turned off redirection for the Error 404 in web.config:

<system.web>
    <customErrors mode="RemoteOnly" defaultRedirect="/error/server-error.aspx">
        <!-- <error statusCode="404" redirect="/error/resource-not-found.aspx"/> -->
    </customErrors>
</system.web>

Then I added the following Application_Error event handler to my Global.asax:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Dim serverError As HttpException = DirectCast(Server.GetLastError(), HttpException)

    If Not serverError Is Nothing Then
        If 404 = serverError.GetHttpCode() Then
            Server.ClearError()
            Server.Transfer("/error/resource-not-found.aspx")
        End If
    End If
End Sub

Finally, I added the lines below to my Error 404. page (resource-not-found.aspx):

Sub Page_Load()
    ' If you're running under IIS 7 in Integrated mode set,
    ' use this line to override IIS errors:
    Response.TrySkipIisCustomErrors = True

    Response.StatusCode = 404
    Response.StatusDescription = "Page not found"
End Sub

FYI: If you use the MasterPage technology, only the variable part of your MasterPage is redirected. After playing with non-existent resources in various sections of our website, I found this behavior smart and useful for our end users.

ONE MORE THING. To return the HTTP 404 Status code for non-ASPX resources, I use the following setting in my web.config:

<system.webServer>
    <httpErrors errorMode="Custom" defaultResponseMode="Redirect">
        <remove statusCode="404"/>
        <error statusCode="404" path="/error/resource-not-found.aspx" responseMode="ExecuteURL"/>
    </httpErrors>
</system.webServer>

Pay attention to the responseMode set to 'ExecuteURL' for the error 404.

0
votes

I have not done a lot with .aspx I was just googling and I'm wondering if you have

<% Response.StatusCode = 404 %>

on the top of you 404.aspx page ?