Afshar
Afshar

Reputation: 11483

Visual Studio 2012 code formatting on CSHTML lowercases generic model types

When formatting code a Razor cshtml file in Visual Studio 2012 (with Ctrl + K + D) if model is a generic type, VS makes it all lowercase. For example:

@model IEnumerable<Content>

converts to (consider all lower case content):

@model IEnumerable<content>

after formatting. This cause the code not to compile. Is this a bug or I'm missing something? How it can be fixed?

Upvotes: 8

Views: 4720

Answers (3)

Brian Ogden
Brian Ogden

Reputation: 19212

The following worked for me with Visual Studio Professional 2012, ASP.NET MVC 5 and Microsoft.System.Web.WebPages 3.0:

<add key="webpages:Version" value="3.0.0.0" />

But this solution did not work immediately

At first, Chris Haddox's solution regarding webpages:Version:

<add key="webpages:Version" value="1.0.0.0"/>

did not work for me, I already had this key and it was set to 1.0.0.0.

But since I am using Visual Studio Professional 2012, ASP.NET MVC 5 and Microsoft.System.Web.WebPages 3.0...

This issue showed me that my Web.Config webpages:Version was set to 1.0.0.0, I changed it to 3.0.0.0 because I read in another article that might be correct for Microsoft.System.Web.WebPages 3.0 and I still had the formatting bug for a little while.

I continued to make edits to my .cshtml file. Then, all of the sudden formatting started working correctly. Maybe from building, but I do not think that I did do a build, but can't be sure, and I know that I did not restart Visual Studio 2012.

Upvotes: 0

If this is a new project in VS 2012, you need to add this line of code in your <appSettings>section of your main web.configfile:

<add key="webpages:Version" value="1.0.0.0"/>

If this was a pre-existing project you probably already have that key in your web.config. Make sure it is for version 1.0.0.0 and not 2.X or 1.2.X

Also ensure that your project reference to System.Web.WebPages is for version 1.0.0.0 and not 2.0.0.0

After you make these changes you have to then close and re-open Visual Studio 2012!

Upvotes: 2

This issue is from Visual Studio 2012 not recognizing it as razor code and treating it as regular HTML. I believe this is a bug with VS 2012 and will hopefully get fixed soon. As a temporary fix, you can do this:

  1. Goto TOOLS -> OPTIONS
  2. Select Text Editor -> HTML -> Formatting
  3. Set the Client Tag drop down to "As entered"

Hope this helps.

Upvotes: 11

Related Questions