在第4和第5行发生长度不能小于零 [英] Length cannot be less than zero error occured at 4th and 5th line
本文介绍了在第4和第5行发生长度不能小于零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1 string userIP = Request.UserHostAddress;
2 string serverIP = Request.ServerVariables [LOCAL_ADDR];
3
4 userIP = userIP.Substring(0,userIP.LastIndexOf(。));
5 serverIP = serverIP.Substring(1,serverIP.LastIndexOf(。));
1 string userIP = Request.UserHostAddress;
2 string serverIP = Request.ServerVariables["LOCAL_ADDR"];
3
4 userIP = userIP.Substring(0, userIP.LastIndexOf("."));
5 serverIP = serverIP.Substring(1, serverIP.LastIndexOf("."));
// only redirect to WinLogin.aspx if user is internal & IE
if (userIP.Equals(serverIP) && Request.Browser.IsBrowser("ie"))
{
Server.Transfer("SecondLogin.aspx"");
}
else
{
Server.Transfer("ThirdLogin.aspx"");
}
推荐答案
userIP和serverIP中包含哪些值。
检查长度是否大于0然后只执行substring语句。
What values coming inside userIP and serverIP.
Put a check if length is greater then 0 then only execute the substring statement.
userIP.Substring(0,userIP.LastIndexOf( 。));
LastIndexOf
如果找不到文本则返回-1。
在您的情况下,在userIP
中找不到'。'并且返回-1。
因此,Substring
失败因为子串(0,-1)
没有意义。
设置一些验证条件以确保在执行子字符串之前存在有效的IP值。
userIP.Substring(0, userIP.LastIndexOf("."));
LastIndexOf
returns -1 if text is not found.
In your case, a '.' is not being found inuserIP
and -1 is being returned.
As a result,Substring
fails becaauseSubstring(0, -1)
makes no sense.
Put some validation conditions to ensure there is a valid IP value before doing a sub string.
这篇关于在第4和第5行发生长度不能小于零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文