【问题标题】:C# Razor Static Helper Error “Collection Was Modified”C# Razor 静态帮助程序错误“集合已修改”
【发布时间】:2016-03-06 20:31:19
【问题描述】:

在我的 MVC Web 应用程序中,我在 /App_Code/Functions.cshtml 中有一个简单的静态 Razor 助手,我用它来编写 HTML 以便在各种不同的视图页面上共享社交媒体:

@helper share (string url = null)
{
    if (url != null)
    {
        string fullUrl = "http://example.com" + url;

        <span class="share">
            <span class="text">Share</span>
            <a href="https://www.facebook.com/sharer.php?u=@fullUrl " class="facebook">Facebook</a>
            <a href="https://www.twitter.com/intent/tweet?url=@fullUrl " class="twitter">Twitter</a>
            <a href="https://plus.google.com/share?url=@fullUrl " class="google">Google+</a>
        </span>
    }
}

但是,我有时会收到错误“收藏已修改;枚举操作可能无法执行”,尤其是在执行站点范围的链接检查时,它总是在设置“fullUrl”变量的帮助程序的第 5 行中断。

由于我在这里没有使用任何集合,我真的不知道为什么会发生这种情况或如何解决它。我怀疑这可能与线程安全有关,但我们将不胜感激。

这是来自错误的示例堆栈跟踪:

at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.BrowserLinkExecutionListener.GetOutputPositionTracker(TextWriter textWriter)
at Microsoft.VisualStudio.Web.PageInspector.Runtime.Tracing.BrowserLinkExecutionListener.BeginContext(PageExecutionContext context)
at CallSite.Target(Closure , CallSite , Object , Object )
at System.Web.WebPages.Instrumentation.PageExecutionListenerAdapter.BeginContext(PageExecutionContextAdapter context)
at System.Web.WebPages.Instrumentation.InstrumentationService.BeginContext(HttpContextBase context, String virtualPath, TextWriter writer, Int32 startPosition, Int32 length, Boolean isLiteral)
at System.Web.WebPages.HelperPage.BeginContext(TextWriter writer, String virtualPath, Int32 startPosition, Int32 length, Boolean isLiteral)
at ASP.Functions.<>c__DisplayClassc.<share>b__b(TextWriter __razor_helper_writer) in \App_Code\Functions.cshtml:line 143
at System.Web.WebPages.HelperResult.WriteTo(TextWriter writer)
at System.Web.WebPages.WebPageExecutingBase.WriteTo(TextWriter writer, HelperResult content)
at System.Web.WebPages.WebPageBase.Write(HelperResult result)
at ASP._Page_Views_News__Article_cshtml.Execute() in \Views\_Article.cshtml:line 134
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
at System.Web.WebPages.StartPage.RunPage()
at System.Web.WebPages.StartPage.ExecutePageHierarchy()
at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult)

更新

我已经复制了这个问题只是做以下......

/App_Code/Functions.cshtml中的Helper方法:

@helper Test ()
{
    @:test
}

空视图页面顶部的方法调用:

@Functions.Test()

当视图被连续多次请求时,就像执行链接检查时一样,每隔一段时间就会发生相同的错误。

【问题讨论】:

  • Functions.cshtml 的第 143 行是什么?
  • string fullUrl = "http://example.com" + url;(这是助手的第 5 行,但 Functions.cshtml 的第 143 行)

标签: c# asp.net-mvc-4 razor helper


【解决方案1】:

您的代码看起来无害,因此请考虑堆栈跟踪。这可能与 PageInspector 有关。这里是a possible fix

核心是在web.config中添加这个app设置;

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

或者在web.config中添加这个&lt;remove assembly=标签;

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

【讨论】:

  • 嗨史蒂夫。非常感谢您的回答,看来问题已经解决了!
【解决方案2】:

只有在迭代列表时修改列表时才会出现错误。例如:

foreach (var item in myList)
{
    myList.Remove(item);
}

会引发此异常,因为枚举的长度会在枚举时被修改。 Add 也会发生同样的情况。您还没有发布任何这样的代码,因此错误发生在此代码之外。在 forforeach 块中查找您可能修改列表的位置,这就是您的问题所在。

【讨论】:

  • 嗨,克里斯。是的,这很有意义,只是在我看来没有任何forforeach!这不是与__razor_helper_writer 以及与之相关的集合有关吗?
  • 它实际上可能不在您的视野中。它可能在控制器中,或者在控制器使用的某个类中,扩展方法或在控制器或视图中调用的东西等。但是,在某个地方,迭代正在发生,这就是你的问题所在。跨度>
  • 空的测试视图也使用了空的控制器,所以这不太可能。根据上面史蒂夫的回答,这似乎是 Visual Studio 及其页面检查器的问题。在已发布的站点构建上运行链接检查没有给出任何错误,它仅在通过本地计算机上的 VS 运行时才中断。
猜你喜欢
  • 1970-01-01
  • 2011-09-12
  • 2011-06-01
  • 2010-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多