得到以下错误“System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度。参数名称:startIndex" [英] getting following error "System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex"

查看:1014
本文介绍了得到以下错误“System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度。参数名称:startIndex"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HttpUsrName =(HttpContext.Current.User.Identity.Name.ToString()。Split('\\')[1]);



分割硬编码值(Domain \\UserName)工作原理

但代码行上面给出错误

System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度。参数名称:startIndex



我已经通过谷歌获得了几个解决方案,但仍未获得任何解决方案。

任何人都可以获得任何解决方案。

HttpUsrName = (HttpContext.Current.User.Identity.Name.ToString().Split('\\')[1]);

splitting of hardcoded value (Domain\\UserName)works
but above code line giving error
"System.ArgumentOutOfRangeException: startIndex cannot be larger than length of string. Parameter name: startIndex"

I have gone through google for several solution but still not get any solution.
Any solution from anyone will be appreciate.

推荐答案

假设Split函数返回的字符串数组中有两个元素,你接受第二个元素(索引1)。



但是,如果数组只有零个或一个元素,你会得到一个ArgumentOutOfRangeException,告诉你你试图访问一个包含少于两个元素的数组的第二个元素。 $>


解决方案:验证您尝试访问的阵列是否真实你认为它的长度。

类似于:

You assume there will be two elements in the string array returned by Split function, and you take the second element (index 1).

But, if the array has zero or one element only, you get an ArgumentOutOfRangeException, telling you that you tried to access the second element of an array that holds less than two elements.

Solution: validate that the array you are trying to access is actually of the length you think it is.
Something like:
string[] parts = HttpContext.Current.User.Identity.Name.ToString().Split('\\');
if (parts.Length == 2) {
   HttpUsrName = parts[1];
}
else {
   HttpUsrName = "Problem in username parsing from http context";
}


对此有一些腥:

报告的错误消息表示名称错误的参数是 startIndex ,并且它是大于字符串长度的问题。

这些排除了问题错误是 [1] 索引。

.Split()不有一个 startIndex 参数。



所以要么有一个不同的字符串方法被调用( .Substring()?)未显示,



<的一个属性code> HttpContext.Current.User.Identity.Name 连锁电话这样的方法,在幕后 !!



这不是引起异常的代码!)
There's something "fishy" about this:
The error message, as reported, indicates the name of the erroneous parameter is startIndex and that it is an issue with it being "larger than the length of string".
These rule out the issue of the error being the [1] indexing.
.Split() doesn't have a startIndex parameter.

So either there's a different string method being called (.Substring()?) that isn't shown,
or
one of the properties in the HttpContext.Current.User.Identity.Name chain calls such a method, under the covers!!

(Or this isn't the code that is causing the exception!)


显然'[1]'超出了范围,因为HttpContext.Current.User.Identity.Name.ToString()返回的字符串长度必须为0即空白。
Obviously the '[1]' is out of range because the length of the string returned by HttpContext.Current.User.Identity.Name.ToString() must be 0 ie blank.


这篇关于得到以下错误“System.ArgumentOutOfRangeException:startIndex不能大于字符串的长度。参数名称:startIndex&quot;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆