我在 VS2012 中创建了两个应用程序
现在我打开任何 .cshtml 文件并添加以下代码:
<div>
@if (true)
{
@string.Format("{0}", "test")
}
</div>
它在 Application#1 (mvc3) 中工作得很好,我看到显示了“测试”字样。但它在应用程序#2 (mvc4) 中不起作用。
任何人都可以解释为什么会发生以及应该改变什么?
更新:我刚刚又发现了一件非常奇怪的事情。如果你用 @String.format("some text") 替换 @string.format("some text") 一切正常(注意大写字符串)
最佳答案
我们在升级时遇到了类似的问题......似乎只需写下 @string.Format("{0}", "test")
将不再像在 MVC3 中那样直接写入页面。相反,您必须执行以下操作:
<div>
@if (true)
{
Html.Raw(string.Format("{0}", "test"));
}
</div>
或者
<div>
@if (true)
{
<text>@string.Format("{0}", "test"))</text> //added @ to evaluate the expression instead of treating as string literal
}
</div>
关于ASP.NET MVC 3 和 ASP.NET MVC 4 razor 语法有什么变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12685827/