在MVC4剃刀可能的重大更改,可以固定" @:@" [英] Possible breaking change in MVC4 Razor that can be fixed with "@:@"
问题描述
我最近升级从ASP.NET MVC3(剃刀),以MVC4(Razor2)我的网站,并在此过程中发现了什么好像在剃刀视图引擎重大更改。
I recently upgraded my website from ASP.NET MVC3 (Razor) to MVC4 (Razor2), and in doing so found what seemed like a breaking change in the Razor view engine.
该场景(大大简化)如下图所示。
The scenario (greatly simplified) is shown below.
@model IEnumerable<string>
@{ Layout = null; }
<!DOCTYPE html>
<html>
<body>
<div>
@foreach (var x in Model)
{
@string.Format("Foo bar: {0}", x) // Errors in MVC4/Razor2
}
</div>
</body>
</html>
这工作在MVC3 /剃刀正常,但是在MVC4 / Razor2的的String.Format
行导致的错误:
This works fine in MVC3/Razor, however in MVC4/Razor2 the string.Format
line results in an error of:
在@字符意外的串的关键字。一旦进入code,你不需要preFIX结构,如串以@。
Unexpected "string" keyword after "@" character. Once inside code, you do not need to prefix constructs like "string" with "@".
如果您删除 @
的视图引擎,然后要求你终止的String.Format
行以分号。然而,ReSharper的话警告说(这是正确的):
If you remove the @
, the view engine then demands that you terminate the string.Format
line with a semicolon. However, ReSharper then warns (rightly so):
纯法的返回值不被使用。
Return value of pure method is not used.
这两个补丁我发现这要么使用&LT;文字和GT;
:
The two fixes I've found for this are either to use <text>
:
<text>@string.Format("The value {0}", x)</text>
或使用更好奇的办法 @:@
:
@:@string.Format("The value {0}", x)
这是一个已知的,并记录在的Razor视图引擎的变化?
Is this a known and documented change in the Razor view engine?
推荐答案
似乎是一个错误。它与字符串
:
Seems like a bug. It works with String
:
@foreach (var x in Model)
{
@String.Format("Foo bar: {0}", x)
}
这篇关于在MVC4剃刀可能的重大更改,可以固定&QUOT; @:@&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!