C# Razor Static Helper Error “Collection Was Modified”

BSB

In my MVC web application I have a simple static Razor helper in /App_Code/Functions.cshtml that I use to write out HTML for social media sharing on various different view pages:

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

However, I sometimes get the error “Collection was modified; enumeration operation may not execute” especially when performing a site wide link check for example, and it always breaks on line 5 of the helper where it sets the “fullUrl” variable.

Since I’m not using any collections here, I don’t really know why this is happening or how to fix it. I suspect it could be something to do with thread safety but any help would be greatly appreciated.

This is an example stack trace from the error:

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)

UPDATE

I've replicated the problem just doing the following...

Helper method in /App_Code/Functions.cshtml:

@helper Test ()
{
    @:test
}

Method call at the top of an empty view page:

@Functions.Test()

When the view is requested multiple times in succession as when performing a link check, every so often the same error occurs.

Steve Cooper

Your code looks innocuous, so consider the stack trace. It might be to do with PageInspector. Here's a possible fix

The core is to add this app setting in web.config;

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

Or add this <remove assembly= tag in 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>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C# Razor Static Helper Error “Collection Was Modified”

From Dev

compilation error collection was modified

From Dev

Razor C# template HTML helper?

From Dev

'Collection was modified' error when Deleting Related Entities

From Dev

'Collection was modified' error when Deleting Related Entities

From Dev

Static Collection of Instances C++?

From Dev

Razor htmlListBox helper

From Dev

Razor htmlListBox helper

From Dev

Correct Razor syntax in helper

From Dev

Dictionary<string, bool?> error - Collection was modified; enumeration operation may not execute

From Dev

How to overcome this error:- Collection was modified; enumeration operation may not execute."

From Dev

Dictionary<string, bool?> error - Collection was modified; enumeration operation may not execute

From Dev

Inconsistent error: collection was modified enumeration operation may not execute

From Dev

Static file collection in Django path error

From Dev

C# - Collection was modified; enumeration operation may not execute

From Dev

C# - Collection was modified; enumeration operation may not execute

From Dev

C# SmtpClient SendAsync from Static Helper Method?

From Dev

Equivalent to Razor Section Helper In Sitecore

From Dev

Razor syntax in helper containing HTML

From Dev

Using @section inside Razor Helper

From Dev

IF ELSE html helper in razor view?

From Dev

Using @section inside Razor Helper

From Dev

Collection was modified IGrouping of Invoices

From Dev

Collection was modified in a foreach loop

From Dev

form for helper bad collection

From Dev

Error to use of static and static function in C++

From Dev

Error to use of static and static function in C++

From Dev

C# Razor View Error - Braces Expected

From Dev

For loop index variable modified with helper method?

Related Related

  1. 1

    C# Razor Static Helper Error “Collection Was Modified”

  2. 2

    compilation error collection was modified

  3. 3

    Razor C# template HTML helper?

  4. 4

    'Collection was modified' error when Deleting Related Entities

  5. 5

    'Collection was modified' error when Deleting Related Entities

  6. 6

    Static Collection of Instances C++?

  7. 7

    Razor htmlListBox helper

  8. 8

    Razor htmlListBox helper

  9. 9

    Correct Razor syntax in helper

  10. 10

    Dictionary<string, bool?> error - Collection was modified; enumeration operation may not execute

  11. 11

    How to overcome this error:- Collection was modified; enumeration operation may not execute."

  12. 12

    Dictionary<string, bool?> error - Collection was modified; enumeration operation may not execute

  13. 13

    Inconsistent error: collection was modified enumeration operation may not execute

  14. 14

    Static file collection in Django path error

  15. 15

    C# - Collection was modified; enumeration operation may not execute

  16. 16

    C# - Collection was modified; enumeration operation may not execute

  17. 17

    C# SmtpClient SendAsync from Static Helper Method?

  18. 18

    Equivalent to Razor Section Helper In Sitecore

  19. 19

    Razor syntax in helper containing HTML

  20. 20

    Using @section inside Razor Helper

  21. 21

    IF ELSE html helper in razor view?

  22. 22

    Using @section inside Razor Helper

  23. 23

    Collection was modified IGrouping of Invoices

  24. 24

    Collection was modified in a foreach loop

  25. 25

    form for helper bad collection

  26. 26

    Error to use of static and static function in C++

  27. 27

    Error to use of static and static function in C++

  28. 28

    C# Razor View Error - Braces Expected

  29. 29

    For loop index variable modified with helper method?

HotTag

Archive