"The length cannot be less than zero." on an empty line

I keep getting the above error message even if I comment on the line where the error occurs. Any idea what could be causing this? I tried to rewrite the lines with test values, but I still get the same error.

This works fine in debug mode, only in the deployment that it came up with.

Source:

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
+5
source share
4 answers

text.IndexOf("\\")will return -1if it cannot find the "\" in the string.

You pass -1through a method Substring()that is invalid.

Page.User.Identity.Name , Windows, IIS .

, , .

http://msdn.microsoft.com/en-us/library/ff647405.aspx:

Windows

  • IIS.
  • "".
  • .
  • "".
  • , " " " Windows" .
  • Web.config Web.config , Windows, .

<system.web> ... <authentication mode="Windows"/> ... </system.web>

+14
text.IndexOf("\\")

-1, , 0 -1 .

ASP.net # , , ( ASP ).

:

int SlashPos = text.IndexOf("\\");
if(SlashPos > 0)
    domain = text.Substring(0, SlashPos);
else
    domain = text;
+4

text \\, text.IndexOf("\\") -1, Substring.

, , text, .

int backSlashIndex = text.IndexOf("\\");
domain = (backSlashIndex >= 0) ? text.Substring(0, backSlashIndex) : text;
+4
source

You pass a number less than zero to the call Substring. I doubt your string initialization example in "TEST" has the same problem ...

+1
source

All Articles