C# Razor 静态助手错误 “Collection Was Modified”

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

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

@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>
    }
}

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

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

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

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 Test ()
{
    @:test
}

在空 View 页面顶部调用方法:

@Functions.Test()

当执行链接检查时连续多次请求 View 时,经常会发生相同的错误。

最佳答案

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

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

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

或者添加这个<remove assembly=标签 web.config ;

<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>

关于C# Razor 静态助手错误 “Collection Was Modified”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34045465/

相关文章:

c# - 编译器说我没有实现我的接口(interface),但我实现了?

javascript - 如何将模型字段传递给 View 中的 Javascript 函数?

layout - MVC 3 布局和 Controller

javascript - 如何在 angularjs 中加载时从 div 调用函数

asp.net-mvc-4 - MVC 4 如果经过身份验证,则从登录页面重定向

c# - 在 MVC Controller 中加载 XML

c# - 是否可以通过 HTML 从 jQuery 中的 C#/Razor 获取数据?

c# - 如何在生成时删除 XML 中的类型?

c# - 按名称将一种枚举类型转换为另一种枚举类型

c# - Newtonsoft 中的 "Type specified in JSON is not compatible"用于出现在不同加载程序集中的类