【问题标题】:How Do I Use Custom Error Pages in Visual Basic Express Edition 2008如何在 Visual Basic Express Edition 2008 中使用自定义错误页面
【发布时间】:2014-03-03 07:15:10
【问题描述】:

当互联网关闭或网页不存在时,我不得不让我的网络浏览器的用户看到 Internet Explorer 错误页面,这让我感到厌烦。有没有办法(相对简单地首选)设置我自己的错误页面?

【问题讨论】:

  • 这是使用浏览器控件吗?
  • 你在使用 asp.net....
  • 我相信 OP 在这里使用 WinForms 中的 WebBrowser 控件。原始帖子中没有提到 ASP.NET。这很有意义,因为他们提到了“Internet Explorer 错误页面”和“当互联网关闭时”

标签: vb.net internet-explorer compiler-errors


【解决方案1】:

当页面不存在时(错误 404),您可以在您的主 web.config 文件中建立一个自定义页面,如下所示:

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

您可以看到定义了两种不同的错误页面:一种用于除 404 之外的所有错误 (PageError.aspx),另一种仅用于 404 错误 (PageNotFound.aspx)。显然,它们都可以是同一个页面。

如果用户没有互联网连接或连接由于时间限制被拒绝,由于没有与您的服务器建立连接,您无法管理这种情况。这很明显。

HTTP 状态码列表:http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

这个问题一点都不新鲜:12345678、...下次做一点研究。

【讨论】:

    【解决方案2】:

    您在 web.xml 中设置了这个。配置文件

    <customErrors mode="On">
        <error statusCode="500" redirect="~/Error.cshtml" />
        <error statusCode="404" redirect="~/404.cshtml" />
    </customErrors>
    

    【讨论】:

    • 如何找到 web.config 文件?
    【解决方案3】:

    我假设这是一个使用 WebBrowser 控件的 WinForms 应用程序?如果是这样,这是一个允许向用户显示替代消息的例程:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多