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?
-
Is this using the webbrowser control? – Matt Wilko Feb 06 '14 at 11:14
-
you r using asp.net.... – pankeel Feb 06 '14 at 11:15
-
I believe the OP is using the WebBrowser control in WinForms here. There was no mention of ASP.NET in the original post. This makes mroe sense because they mention "the internet explorer error page" and "when the internet is down" – Matt Wilko Feb 06 '14 at 11:49
3 Answers
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.

- 1
- 1

- 1,026
- 14
- 32
You are set this in web. config file
<customErrors mode="On">
<error statusCode="500" redirect="~/Error.cshtml" />
<error statusCode="404" redirect="~/404.cshtml" />
</customErrors>

- 1,138
- 1
- 10
- 22
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

- 26,994
- 10
- 93
- 143