0

I am sick of having to have my web browser's users see the internet explorer error page when the internet is down or the webpage doesn't exist. Is there a way to (relatively simply is preferred) setup my own error pages?

Matt Wilko
  • 25,893
  • 10
  • 85
  • 132

3 Answers3

1

When a page does not exist (error 404), you can establish a custom page in your main web.config file as follow:

<customErrors mode="RemoteOnly" defaultRedirect="~/PageError.aspx">
  <error statusCode="404" redirect="~/PageNotFound.aspx" />
</customErrors>

You can see there are two different error pages defined: one for all errors but the 404 (PageError.aspx) and one just for 404 errors (PageNotFound.aspx). Obviously, they both can be the same page.

In case the user have not an internet connection or the connection have been refused due time limit, you cannot manage the situation due there is not a connection established with your server. And that is pretty obvious.

List of HTTP status codes: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

This question is not new at all: 1, 2, 3, 4, 5, 6, 7, 8, ... So, please, do a little research next time.

Community
  • 1
  • 1
adripanico
  • 1,038
  • 14
  • 31
0

You are set this in web. config file

<customErrors mode="On">
    <error statusCode="500" redirect="~/Error.cshtml" />
    <error statusCode="404" redirect="~/404.cshtml" />
</customErrors>
pankeel
  • 1,128
  • 1
  • 9
  • 22
0

I am assuming this is a WinForms app using a WebBrowser control? If so here is a routine that will allow an alternative message to be shown to the user:

Private Sub NavigateToPage(page As String)
    Dim newUrl As String
    Dim customHtmlPageFileName As String = Path.Combine(Path.GetTempPath, "NoConnection.htm")

    If Not CanReachPage("Http://www.google.com") Then
        Dim customHtmlPageData As String = "<html>No Internet Connection</html>"
        My.Computer.FileSystem.WriteAllText(customHtmlPageFileName, customHtmlPageData, False)
        newUrl = "File://" + customHtmlPageFileName
    ElseIf Not CanReachPage(page) Then
        Dim customHtmlPageData As String = "<html>Page Not Found</html>"
        My.Computer.FileSystem.WriteAllText(customHtmlPageFileName, customHtmlPageData, False)
        newUrl = "File://" + customHtmlPageFileName
    Else
        newUrl = page
    End If
    WebBrowser1.Navigate(newUrl)
End Sub

Public Shared Function CanReachPage(page As String) As Boolean
    Try
        Using client = New WebClient()
            Using stream = client.OpenRead(page)
                Return True
            End Using
        End Using
    Catch
        Return False
    End Try
End Function
Matt Wilko
  • 25,893
  • 10
  • 85
  • 132