System.InvalidOperationException: 栈为空。

25

当运行在Visual Studio 2013中构建的ASP.NET 4.x应用程序时,我遇到了以下异常。

我尝试通过删除页面检查器程序集来禁用PageInspector。

<assemblies>
    <remove assembly="Microsoft.VisualStudio.Web.PageInspector.Loader, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</assemblies>

并将此配置添加到应用程序设置中

<appSettings>
    <add key="PageInspector:ServerCodeMappingSupport" value="Disabled"/>
</appSettings>

什么都没帮上忙。

[/Pages/TargetPage.aspx] System.Web.HttpException (0x80004005): Exception of type 'System.Web.HttpException' was thrown. ---> System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Stack empty.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.Stack`1.Pop()
   at Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter writer, Object renderedObject)
   at System.Web.UI.RenderTraceListener.RenderTraceListenerList.EndRendering(TextWriter writer, Object renderedObject)
   at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
   at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.LegacyPageAsyncInfo.<CallHandlersPossiblyUnderLock>b__32(Object o)
   at System.Web.HttpContext.InvokeCancellableCallback(WaitCallback callback, Object state)
   at System.Web.UI.Page.LegacyPageAsyncInfo.CallHandlersPossiblyUnderLock(Boolean onPageThread)
   at System.Web.UI.Page.LegacyPageAsyncInfo.CallHandlers(Boolean onPageThread)
   at System.Web.HttpAsyncResult.End()
   at System.Web.UI.Page.LegacyAsyncPageEndProcessRequest(IAsyncResult result)
   at System.Web.UI.Page.AsyncPageEndProcessRequest(IAsyncResult result)

有没有办法避免这个异常?感谢任何建议。

13个回答

26

4
在Visual Studio 2017中也很有帮助。 - Stephan Bauer
这是一个失效的链接。这里有一个可用的链接:https://dev59.com/o2Ag5IYBdhLWcg3w1uDy - David

25
这可能是因为标签上的AssociatedControlID与找不到的控件相关联。

9
有多个原因导致了这种情况。
System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. -
System.InvalidOperationException: Stack empty.
Microsoft.VisualStudio.Web.PageInspector.Runtime.WebForms.SelectionMappingRenderTraceListener.EndRendering(TextWriter writer, Object renderedObject)
...

添加建议后:

 <appSettings>
   <add key="PageInspector:ServerCodeMappingSupport" value="Disabled"/>
   ...
 </appSettings>

我注意到在下拉列表中选择了重复的选项,导致出现问题。

7

希望这对某人有所帮助。

当我的asp:panel有一个名为XYZ的默认按钮,并且我修改了名称XYZ,但忘记在面板中更改默认按钮名称时,就会出现此错误。


4
我找到了问题所在。GridView中控件ID生成的方式已经从默认更改为:
ClientIDMode="Predictable" ClientIDRowSuffix="ID"

导致异常的原因不确定,可能是PageInspector栈为空时隐藏了实际异常。将更改还原为原始状态即可解决该问题。

现在出现了另一种异常(但一旦GridView设置恢复到原始状态,这个异常也会消失)

System.ArgumentException: An entry with the same key already exists.

11
谢谢,添加 <add key="PageInspector:ServerCodeMappingSupport" value="Disabled"/> 解决了我的问题! - GôTô
@mybrave:你能否为“PageInspector:ServerCodeMappingSupport”键编写一个答案,因为这似乎是相当多人(包括我自己)的解决方案。 - CodeZombie

3

我还在一些使用启用“浏览器链”功能的Visual Studio 2013开发机上运行的Web应用程序中看到了这个问题。

在我看到的多种情况下,保留浏览器链接会导致各种问题,包括客户端脚本和安全警告以及像此类的服务器异常(令人讨厌的是它似乎默认启用,谢谢Msft!)


2

我也遇到了同样的异常,但我的问题是缺少以下内容:

<form runat="server">
</form>

2
在我的情况下(ASP.NET 4.6,Visual Studio 2015),我注释了一个asp文本控件,并添加了一个新的asp下拉列表,但没有更新相关标签的AssociatedControlID。
将AssociatedControlID更改为新添加的asp下拉列表ID后,我的问题得到解决。

1
当我向页面添加一个新的<asp:Panel>并将DefaultButton设置为面板外的按钮(错误地)时,发生了这种情况。当我将DefaultButton更改为面板内的另一个按钮时,错误得到了修复。

1
在我的情况下,我已经移除了一个 asp:DropDownList,并且有一个与其 ID 相关联的 asp:Label,使用了 AssociatedControlID。在移除相关控件 ID 后,它起作用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接