【发布时间】:2015-02-12 03:48:27
【问题描述】:
过去几年我一直在使用 Visual Studio 2010,但今天我们需要更新到 2013。我去打开我现有的项目,该项目之前没有任何问题,但现在抛出以下错误:
A first chance exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll
Additional information: startIndex cannot be larger than length of string.
在这行代码触发了这个错误:
string logon_user = Request.ServerVariables["LOGON_USER"].Substring(7);
关于如何解决这个问题的任何想法?经过一番研究,我找到了一个简单的解决方法,改用这一行:
string logon_user = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Substring(7);
但我更感兴趣的是找出 Request.ServerVariables 不再起作用的原因。我在大量其他项目中使用它,并想看看我是否可以在不改变每个项目的线路的情况下解决它。
【问题讨论】:
-
Request.ServerVariables["LOGON_USER"]的值究竟是多少?你调试过你的代码吗? -
错误说明了这一点。我认为您的用户名不超过 7 个字符
-
@SonerGönül 该值为 NULL,如果我现在运行它,我无法弄清楚为什么它会在 2010 年拉入我的用户名。
-
@Codeek 我明白这一点,但我的问题是为什么 ServerVariables 不再适用于 VS 2013,是否有变通办法让它工作。
-
我们需要更多的上下文。你到底在哪里打这个电话?例如在 application_start 中吗?
标签: c# asp.net visual-studio-2010 visual-studio