我们正在将 .NET mvc 应用程序从 MVC3 升级到 MVC5,其中包括从 Razor 1 升级到 Razor 3。我们在 View 中不断遇到的一个错误如下:
在 MVC3 中,我们的许多 View 都是这样做的:
@if (someCondition)
{
@* render some content *@
@string.Format(...)
}
但是,在 MVC5 中,这会产生以下错误:
System.Web.HttpParseException: Unexpected "string" keyword after "@" character. Once inside code, you do not need to prefix constructs like "string" with "@".
据我所知,有两个简单的修复方法:
@if (someCondition)
{
@* render some content *@
@: @string.Format(...)
}
或者
@if (someCondition)
{
@* render some content *@
<text> @string.Format(...) </text>
}
但是,在数百个 View 中进行和测试这些更改会很痛苦。因此我的问题是:解决这个看似重大变化的最佳方法是什么?
- 是否有某种标志我们可以打开,使 Razor 的行为像以前一样?
- 对于 Razor 3 中的这种结构,推荐的风格是什么?
最佳答案
我认为 this question 的答案可能会提供一些帮助。虽然我不知道,也可能没有,你可以在设置中打开一个特定的标志,如果你只是用 @String 替换 @string (因此使用类而不是别名),它似乎只是工作很好。
@if (condition)
{
@String.Format("test {0}", 2992)
}
希望这会像在代码中用“@String”查找/替换“@string”的任何实例一样简单。
关于c# - 解决 Razor 重大变化的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22937758/