0
public void GrantAccesOnPage(int AccessPoint1, int AccessPoint2) 
        {
            GlobalVariables.GrantDeny = 0;
            string[] arr = { 
                               GlobalVariables.SessionPort1, 
                               GlobalVariables.SessionPort2, 
                               GlobalVariables.SessionPort3, 
                               GlobalVariables.SessionPort4 
                           };
            foreach (var r in arr)
            {
                if (Convert.ToInt32(r) == AccessPoint1 || Convert.ToInt32(r) == AccessPoint2)
                {
                    if (!string.IsNullOrEmpty(r.ToString()))
                    {
                        GlobalVariables.GrantDeny = 1;
                    }
                    else 
                    {
                        throw new System.ArgumentException("Null");
                    }
                }
            }
            if (GlobalVariables.GrantDeny != 1)
            {
                Response.Redirect("PageNotAccessable.aspx");
            }
        }

我尝试将我的“r”foreach 语句中的每个“Var”转换为字符串或整数,但仍然没有捕获空值尝试抛出异常也不会跳过,如果它的空值将页面停止到错误视图中。请帮忙..

4

1 回答 1

1

您应该首先检查 IsNull:

public void GrantAccesOnPage(int AccessPoint1, int AccessPoint2) 
        {
            GlobalVariables.GrantDeny = 0;
            string[] arr = { 
                               GlobalVariables.SessionPort1, 
                               GlobalVariables.SessionPort2, 
                               GlobalVariables.SessionPort3, 
                               GlobalVariables.SessionPort4 
                           };
            foreach (var r in arr)
            {
                if (string.IsNullOrEmpty(r))
                {
                    //remove the comment out only if you want to throw the exception. 
                    //throw new System.ArgumentException("Null");  
                }
                else
                {    
                   if (Convert.ToInt32(r) == AccessPoint1 || Convert.ToInt32(r) == AccessPoint2)
                   {
                        GlobalVariables.GrantDeny = 1;                    
                   }
                }
            }
            if (GlobalVariables.GrantDeny != 1)
            {
               Response.Redirect("PageNotAccessable.aspx");
            }
        }
于 2013-10-04T08:57:19.703 回答