【问题标题】:Index was outside the bounds of the array (500th?)索引超出了数组的范围(第 500 个?)
【发布时间】:2012-09-18 03:22:21
【问题描述】:

在加载时调用函数并且错误指向它。仅在发布时调试时,我没有得到索引超出范围错误。

    {
        string strLogonUser = Request.ServerVariables["LOGON_USER"];
        var credential = strLogonUser.Split(@"\".ToCharArray())[1];




        cnn.Open();
        SqlTransaction tran = cnn.BeginTransaction();
        cmd.Connection = cnn;
        cmd.Transaction = tran;
        cmd.CommandText = "StoredProcedure";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@UserLogin", SqlDbType.NVarChar).Value = credential;
        cmd.ExecuteNonQuery();


    }




    [IndexOutOfRangeException: Index was outside the bounds of the array.]
   _Default.ReadUser() +357
   _Default.Page_Load(Object sender, EventArgs e) +46
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +42
   System.Web.UI.Control.OnLoad(EventArgs e) +132
   System.Web.UI.Control.LoadRecursive() +66
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2428

【问题讨论】:

  • 在调试时 LOGON_USER 是否不同?更具体地说,另一个不包含'\'吗?
  • 您为什么不检查一下是否有一个“\”,我还建议将该代码放在 Try{}Catch{} 周围 \

标签: c# asp.net


【解决方案1】:

如果LOGON_USER 服务器变量不包含反斜杠,那么您肯定会收到该错误...您将采用单元素数组的元素 1(第二个元素)。

听起来您应该在拆分之前添加更多日志记录以记录值。

【讨论】:

    【解决方案2】:

    您很可能允许匿名访问,因此该变量中有一个空白字符串

    【讨论】:

    • 测试服务器没有正确设置。
    【解决方案3】:

    您能否为"LOGON_USER" 变量添加检查:

    string strLogonUser = Request.ServerVariables["LOGON_USER"];
    
    if(String.IsNullOrEmpty(strLogonUser))  // <<<<<<
        throw new ArgumentException();  
    
    var credential = strLogonUser.Split(@"\".ToCharArray())[1];
    

    好像没有设置这个变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      • 1970-01-01
      • 2015-11-06
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多