Dusda
Dusda

Reputation: 3367

No highlighting in most pages, reports error when editing

I'm in the middle of upgrading a solution from VS2010/MVC3/.Net 4 to VS2012/MVC4/.Net 4.5. The solution has been upgraded using VS2012's project migration tool and I followed this guide to upgrade MVC3 to 4.

At the moment, Razor is giving me problems. Syntax highlighting doesn't appear for anything but the layout page, and when I try to move around in a view I either get the dialog:

Waiting for a background operation to complete. This dialog will close when the operation completes.

Or I get an error telling me to check the Visual Studio activity log (C:\Users{User}\AppData\Roaming\Microsoft\VisualStudio\11.0\ActivityLog.xml), which led me to this error:

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.VisualStudio.Web.HTML.Implementation.Projection.GrowingSpanTracker.EnsureNoOverlap() at Microsoft.VisualStudio.Web.HTML.Implementation.Projection.GrowingSpanTracker.EnsureTrackingPoints() at Microsoft.VisualStudio.Web.HTML.Implementation.Projection.GrowingSpanTracker.OnTextBufferChanged(Object sender, TextContentChangedEventArgs e) at Microsoft.VisualStudio.Text.Utilities.GuardedOperations.RaiseEvent[TArgs](Object sender, EventHandler`1 eventHandlers, TArgs args)

The Source column of the log says it comes from a "Editor or Editor Extension". I'm running vanilla VS2012 here, with no extensions aside from the first party stuff (Microsoft Web Developer Tools, NuGet Package Manager, and Visual Studio Extensions for Windows Library for JavaScript).

EDIT: Some additional details. If I create a new solution and MVC4 project, add the line:

@RenderSection("title", false)

to the layout, and then attempt to define the section in a view:

@section title{Stuff}

The moment I start typing "Stuff" within the braces I get the same errors/behavior.

Upvotes: 1

Views: 380

Answers (1)

Dusda
Dusda

Reputation: 3367

Turns out whatever changed in Razor made braces a bit more...sensitive. If you have a section defined in your layout like this:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@RenderSection("title", false) - MyApp</title>
        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
        <meta name="viewport" content="width=device-width" />
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
        @RenderSection("css",false)
    </head>
    ...

And then, in a view that uses the layout, try to use that section exactly like this (make sure you actually type it, don't copy/paste):

@model MyApp.Web.Models.HomeIndexModel
@section title {Lovely Title}

Razor will throw a fit and toss an error into your Visual Studio activity log. Highlighting and most Intellisense support will also fail to work. After some trial and error, I found that it works fine if you basically never leave the braces on the same line. So, write it like this:

@model MyApp.Web.Models.HomeIndexModel
@section title {
    Lovely Title
}

And it will work fine.

Upvotes: 2

Related Questions