0

我总是得到错误:

 A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

现在,我想将我的项目重定向到一个页面,该页面显示消息“我们很快就会回来”或出现的错误消息旁边的其他内容。

谁能告诉我如何在我的 asp.net mvc 2.0 项目中做到这一点?

谢谢。

4

1 回答 1

2

您可以Application_Error在您的Global.asax

protected void Application_Error()
{
    var exception = Server.GetLastError();
    var sqlException = exception as SqlException;
    // See http://msdn.microsoft.com/en-us/library/cc645603.aspx 
    // for a full list of error numbers that you might be interested in
    if (sqlException != null && sqlException.Number == 2)
    {
        // it was a SqlException with error number = 2 meaning that this
        // is the exception that you wanted to handle => we redirect to some page
        Response.Clear();
        Server.ClearError();
        Response.Redirect("~/Offline.htm");
    }
}
于 2012-07-19T08:19:10.547 回答