【问题标题】:How to redirect to ASP.NET Error Pages when Session Sql database is downSession Sql 数据库关闭时如何重定向到 ASP.NET 错误页面
【发布时间】:2018-06-26 06:42:49
【问题描述】:

我有一个在 SQL Server 中存储会话的 ASP.NET 网站。我特意让我的会话数据库离线,以确保我的错误页面能够显示给用户。

我的 Web.config :

 <system.webServer>
  <httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="13" />
    <error statusCode="404" subStatusCode="13" prefixLanguageFilePath="" path="LargeFileError.aspx" responseMode="Redirect" />
      <remove statusCode="500" />
      <error statusCode="500" path="Error.aspx" responseMode="ExecuteURL"/>
  </httpErrors>
 </system.webServer>

 <system.web>
  <customErrors mode="On" defaultRedirect="/Error.aspx" redirectMode="ResponseRewrite">    
    <error statusCode="500" redirect="Error.aspx" />
  </customErrors>
</system.web>

我的 global.asax:

protected void Application_Error(object sender, EventArgs e)
        {
           Exception exception = Server.GetLastError();
             Response.Clear();
            Server.ClearError(); 
           Response.Redirect("Error.aspx");     
        }

我的 Error.aspx 页面是一个简单的页面,它只有一个本地化字符串。因此,将无法使其成为 html 页面。

我不断收到此错误:

“底层提供程序在打开时失败。”

你可能会说这个错误有很多关于堆栈溢出的解决方案,但我这里的问题是显示错误页面(会话数据库关闭时的 aspx 页面)而不是解决这个错误。

即使我直接导航到 error.aspx 页面,我也会收到此错误。 我需要一种方法来告诉 Asp.Net & IIS 我不需要此特定页面的会话信息。

如何解决这个问题并确保显示我的错误页面。

【问题讨论】:

    标签: c# asp.net iis


    【解决方案1】:

    在现有的连接字符串中去掉“user Instance=true”就可以了。

    在此处查看详细信息: https://blogs.msdn.microsoft.com/dataaccesstechnologies/2012/08/09/error-the-underlying-provider-failed-on-open-in-entity-framework-application/#comment-17785

    【讨论】:

    • 我不需要解决错误信息,因为我故意使我的数据库脱机。我希望用户被重定向到我的错误页面。
    【解决方案2】:

    所以我做了这些事情:

    1) 在 Global.asax 中将 Respose.redirect 更改为 Server.Transfer

    2) 在Error.aspx 的@Page 指令上添加了这个设置

    EnableSessionState="false"
    

    3) 我还必须管理我的站点。Master(Error.aspx 是一个子页面)有一个 Anti Xsrf 验证,它抛出了错误。

      if (Context.User != null)
                    {
                        ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
                    }
                    else {
                        ViewState[AntiXsrfUserNameKey] = String.Empty;
                    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-09
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      相关资源
      最近更新 更多