Why is my razor view complaining “} expected” afte

2019-03-17 19:56发布

问题:

I have an existing razor 1 / mvc 3 view, with a few nested if - very simple, but after upgrading to razor 2 / mvc 4 it is complaining at runtime and compile (BuildViews) about } expected. It used to work fine.

  • I've checked all the elements are well-formed / closed
  • I've checked that all the { / } are balanced - this isn't the end-of-file issue

What can be wrong?

回答1:

A number of legacy razor oddities were fixed in the razor 2 / mvc 4 upgrade; one interesting gotcha is that in razor 1, the following is legal (in a code region):

string foo = @Some.Complex.Expression;

Note that the @ there is superfluous and incorrect, but that razor 1 does not complain. However, razor 2 is more fussy and gets confused, reporting the } expected error. This line should be replaced with:

string foo = Some.Complex.Expression;

This is a bit subtle, because the error that occurs has nothing to do with braces ({/}), and can be some lines away from the reported line.