C#Razor Static Helper错误“集合已被修改”

时间:2015-12-02 14:39:49

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

在我的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>
    }
}

然而,我有时会收到错误“收集被修改;枚举操作可能不会执行“特别是在执行站点范围链接检查时,例如,它总是在帮助器的第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方法:

@helper Test ()
{
    @:test
}

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

@Functions.Test()

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

2 个答案:

答案 0 :(得分:2)

您的代码看起来无害,因此请考虑堆栈跟踪。它可能与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>

答案 1 :(得分:0)

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

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

会引发此异常,因为枚举的枚举长度正在被枚举。 Add会发生同样的情况。您还没有发布任何类似的代码,因此错误发生在此代码之外。查找您可能在forforeach块中修改列表的位置,以及您遇到问题的位置。