1

I am checking for a valid page in my Global.asax file like this:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request

    Dim path As String = HttpContext.Current.Request.Path.ToUpper

    If Not (path.EndsWith(".ASPX") OrElse path.EndsWith(".PNG") OrElse path.EndsWith(".GIF") OrElse path.EndsWith(".MASTER") OrElse path.EndsWith(".JS") OrElse path.EndsWith(".CSS") OrElse path.EndsWith(".PDF")) Then
        'invalid page
        Response.Redirect("/ErrorPageNotFound.aspx?aspxerrorpath=" + HttpContext.Current.Request.Path)

    End If
End Sub

And I have modified by web.config to include the line:

<error statusCode="404" redirect="ErrorPageNotFound.aspx"/>

This works fine when running under Visual Studio Development Server or UltiDev Web Server, but when I use Local IIS Web Server in Visual Studio or full blown IIS and try to get to the nonexistant foo.php I get the standard IIS 404 error page. Note that if I navigate to foo.aspx I get my custom error page.

It seems as if IIS is seeing the request before it gets to my code and deciding to reroute it? Is there any way round this?

1
  • Is the IIS set up to direct the file types to .NET? Commented Nov 27, 2013 at 18:04

1 Answer 1

3

You are just handling 404 errors for asp.net, php, png and all other non-dot-net files are not affected by this.

You need to set 404 pages on the IIS level to handle all invalid requests including ones for asp.net.

for example in your web.config add:

<system.webServer>
  <httpErrors existingResponse="Auto" errorMode="Custom" defaultResponseMode="File">
    <remove statusCode="404" subStatusCode="-1"/>
    <error statusCode="404" path="404.html"/>
  </httpErrors>
</system.webServer>
Sign up to request clarification or add additional context in comments.

3 Comments

I have no control over the actual IIS Server as the site is hosted elsewhere, so I really need a solution I can do in my project if possible
@MattWilko - You don't need to have access to the IIS server, just override the global IIS error pages in your own web.config. I added an example. It is possible that your hoster prevents this setting in your web.config, but by default it is possible.
Thanks I managed to get this working locally on my IIS Express but not on the host server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.