"Length cannot be less than zero." on a blank line
I keep getting the above error message, even if I comment out the开发者_如何学Python line that the error is occurring on. Any idea what could be causing this? I've tried re-writing the lines with test values, but I still get the same error.
This works perfectly in debug mode, it's only in the deployment that this came up.
Original code:
Line 21: string domain, username;
Line 22: string text = Page.User.Identity.Name;
Line 23:
Line 24: domain = text.Substring(0, text.IndexOf("\\"));
Line 25: username = text.Substring(text.IndexOf("\\") + 1, text.Length - text.IndexOf("\\") - 1);
Source File: F:\<file path>\Default.aspx.cs Line: 23
Test code (same error):
Line 21: string domain, username;
Line 22: //string text = "TEST"; // Page.User.Identity.Name;
Line 23: // this line is blank
Line 24: domain = "TEST"; //text.Substring(0, text.IndexOf("\\"));
Line 25: username = "TEST"; // text.Substring(text.IndexOf("\\") + 1,
Source File: F:\<file path>\Default.aspx.cs Line: 23
Here's the stack trace if it helps at all:
[ArgumentOutOfRangeException: Length cannot be less than zero.
Parameter name: length]
System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +12681546
Insufficiencies._Default.Page_Load(Object sender, EventArgs e) in F:\<file path>\Default.aspx.cs:23
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +25
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3048
text.IndexOf("\\")
will be returning -1
if it cannot find "\" in the string.
You are passing -1
through to the Substring()
method, which is invalid.
The Page.User.Identity.Name
will return an empty string if the site is not run with windows integrated authentication exclusively enabled in IIS for that site.
Users will likely be accessing the site under anonymous authentication.
From http://msdn.microsoft.com/en-us/library/ff647405.aspx:
To configure Windows authentication
- Start Internet Information Services (IIS).
- Right-click your application's virtual directory, and then click Properties.
- Click the Directory Security tab.
- Under Anonymous access and authentication control, click Edit.
- Make sure the Anonymous access check box is not selected and that Integrated Windows authentication is the only selected check box.
- In your application's Web.config file or in the machine-level Web.config file, ensure that the authentication mode is set to Windows as shown here.
<system.web>
...
<authentication mode="Windows"/>
...
</system.web>
The variable text
contains no \\
substring thus text.IndexOf("\\")
returns -1 which is indeed invalid argument for Substring
.
To fix this, you can use such code that will assign the whole text
when backslash is not found.
int backSlashIndex = text.IndexOf("\\");
domain = (backSlashIndex >= 0) ? text.Substring(0, backSlashIndex) : text;
text.IndexOf("\\")
This will return -1 if the index of the characters is not found, and taking a substring from character 0
with length of -1
will throw that error.
Another caveat of ASP.net c# is that the length parameter of a sub string also can't be larger than the actual string (Classic ASP lets you do this).
Try this:
int SlashPos = text.IndexOf("\\");
if(SlashPos > 0)
domain = text.Substring(0, SlashPos);
else
domain = text;
You are passing a number less than zero into the Substring
call. I doubt your example of initializing the string to "TEST" has the same problem...
Please review the code snippet below:
string newString = "10,20,...";
index1 = 0;
string fragment = " ";
while (newString.Contains(",") == true)
{
index1 = newString.IndexOf(",");
fragment = newString.Substring(0, index1);
MessageBox.Show("fragment:" + fragment);
newString = newString.Substring(index1 + 1);
MessageBox.Show("newString:" + newString);
}
We can use the "string.Contains" function and add a tag to the string. Addition of the tag can be done through the code as well. The out put:
"fragment: 10 newString: 20,.... fragment: 20; newString:...."
The while loop terminates here without showing any exception.
精彩评论