Alex Rivera | Logout

Get Visual Studio to highlight Razor syntax with a custom host factoryType

Asked 2012-09-12T00:53:49.917
10

Here's the scenario, I've extended MvcWebRazorHostFactory so I can do a little bit of extra view magic at build time. The exact magic doesn't matter.

The registration in my ~\Views\Web.config looks like so:

<host factoryType="StackExchange.MyNamespace.MyFactory, StackExchange.MyNamespace" />

There's to the appropriate assembly in the project, a copy of the assembly in a \lib folder, and I've confirmed that it's copied to \bin as expected. Furthermore, the actual view magic does happen when views are built so ASP.NET itself is actually finding everything.

What doesn't work in Visual Studio (2012)'s Razor Syntax Highlighting.

enter image description here

Above is a snapshot of how the syntax highlighting fails. As a rule, anything not in a namespace directly @using'd cannot be found (and thus gets the red squiggly underline) and the @model directive doesn't works. This points to some trouble getting at our automatically included namespaces in the Web.config.

The tooltip for the error on @model is "The name 'model' does not exist in the current context".

Through trial and error I've narrowed the root cause to the above host config section, although we have some other tweaks around Razor (a custom pageBaseType for example) it's that one line that breaks everything.

Things I've tried thus far:

  • Strong naming the assembly
  • Inst
Edit
Report

1 Answer

6

After some discussion with Microsoft, we found the root cause of this problem.

As usual, it was my code.

The root error was an MVC3 reference in the custom factoryType, despite the project (and everything around it) being MVC4.

To sum everything up, to get Visual Studio IntelliSense working in .cshtml files with a custom <host factoryType>:

  • You must strong name your assembly
  • You must install the assembly in the GAC
    • We still debug and deploy against a local copy, but VS will look in the GAC
  • You must have all the right references*

Be aware that you'll need to restart Visual Studio after installing into the GAC, simply reloading the solution is not (usually) sufficient. We've also found that some (but not all) ReSharper installations will need their cache's cleared, to be on the safe side clear the cache.

One minor trip up when adding a reference to an assembly that resides in the GAC is the Visual Studio won't copy it to your output directory by default, this may work for you but caused problems for us. You can change this in the properties of the reference.

enter image description here

Also, Visual Studio will give you a number error messages (the "Other Error" purple underline I was unable to reliably reproduce earlier) if your custom factory throws any exceptions; except perhaps in it's constructor (that seemed a little inconsistent).

Where you get no help is the case we were in, where the type itself has serious issues. It was just a coincidence that things worked at runtime (the interfaces we're playing with didn't change between MVC3 and MVC4, and weak naming did the rest).

*If

answered 2012-09-13T19:41:15.643

Your Answer