Unexpected "<XXX>" keyword after "@" character

Topics: ASP.NET Web Pages
Oct 14, 2013 at 11:00 AM
Hi,

I am trying to upgrade from MVC3 to MVC4 and have stumbled across some issues that worked in MVC3 but is broken in MVC4. One of these are that an exception is arised when a C# keyword (string, int etc) is used after a @ character.

For example:
@{
int test = 1;

@string.Format("{0}", test)
}

when running this code I get a Parser error stating Unexpected "int" keyword after "@" character. If I use the class eqvialent of string (String) it works.

I just wonder why the support for having keywords after @ was removed?

I have tried to search for an explanation of this (breaking) change, but cannot find any, the only thing I found was a StackOverflow post indicating it as a bug: http://stackoverflow.com/questions/15543841/possible-breaking-change-in-mvc4-razor-that-can-be-fixed-with.
Coordinator
Oct 14, 2013 at 6:35 PM
Hey @dozer,

I replied the stackoverflow thread as well.

The issue is indeed a bug https://aspnetwebstack.codeplex.com/workitem/458 which we decide not to fix (see more details in the bug).

But in reality the @string was not required and is not correct in that case. There are two main uses for @
  1. Specify a transition from Markup to C#, in this case you are already in C#
  2. Override the use of a keyword (e.g. define a variable named class: int @class)
In your example you are already in C#, and you are not intending to use @string as a variable, so the code is basically wrong. It just happened to work in V1. We did not intend to make a breaking change, but we also don't think it's important enough to fix.

in the stackoverflow thread it is also mentioned that resharper warns if you are not using the @String, well that seems to be a bug in resharper as the resulting code is the same (@String. or String. or string.).
Marked as answer by dozer on 10/14/2013 at 11:48 AM
Oct 14, 2013 at 6:49 PM
Hi!

Thanks for Your answer, it was what I suspected, but I had to be sure.
Coordinator
Oct 14, 2013 at 9:20 PM
By the way, check out this cool tool by Andrew Nurse to visualize actual generated c# code: https://github.com/anurse/RazorSpy