登录后尝试重定向

时间:2014-01-15 18:30:49

标签: c# asp.net redirect

我有一个复杂的情况,试图让login.aspx将您重定向到登录前的Web表单。我正在使用ASP.NET / C#来编写我的Web应用程序。这听起来很容易,但是我遇到了各种各样的问题,我尝试了很多不同的方法。以下是login.aspx.cs中显示重定向的部分:

    HttpContext.Current.ApplicationInstance.CompleteRequest();
    string url = HttpContext.Current.Request.Url.AbsoluteUri;
    try{
        int start = url.IndexOf('=');
        if(url.Length > 0)
        {
            url = url.Substring(start, url.Length - 1 - start);
            url = url.Replace("=","");
        }
        Response.Redirect(url, false);
    }
    catch(Exception i)
    {}

上面的代码获取url字符串并提取redirectURL变量(前一个webform的地址),并在登录后使用它来重定向。 此try-catch中没有异常,但在登录尝试重定向到上一个Web表单后发生异常。这是一个带有导向登录代码的Web表单:

    var returnUrl = Request.Url.PathAndQuery;
    Response.Redirect("~/Account/login.aspx?ReturnURL=" + returnUrl);

问题是我在浏览器中遇到SQL异常?我在这个重定向期间甚至没有使用SQL。这是异常所说的:

   [SqlException (0x80131904): 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: SQL Network Interfaces, error: 26 - Error Locating   Server/Instance Specified)]
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) +5066458
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning() +234
    System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo,   SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire,   Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity) +341
    System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject) +129
    System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout) +270
    System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance) +195
    System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance) +232
    System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection) +5080107
    System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup) +31
    System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +76
    System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126
    System.Data.SqlClient.SqlConnection.Open() +125
    System.Web.Management.SqlServices.GetSqlConnection(String server, String user,  String password, Boolean trusted, String connectionString) +82

   [HttpException (0x80004005): Unable to connect to SQL Server database.]
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +137
   System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +94
   System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +27
   System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +395

也是源错误:

    An unhandled exception was generated during the execution of the current web
    request. Information regarding the origin and location of the exception can be
    identified using the exception stack trace below (edit: above actually).

我的猜测是,由于某种原因,重定向找不到网络表单。我确实尝试使用和不使用〜开始的returnURL变量字符串,看看是否有效,但没有。我也试过会话变量和cookie ...仍然不想工作。提前感谢您的帮助!

0 个答案:

没有答案