MVC4 Razor 中可能的重大更改,可以通过“@:@"修复 [英] Possible breaking change in MVC4 Razor that can be fixed with "@:@"

查看:27
本文介绍了MVC4 Razor 中可能的重大更改,可以通过“@:@"修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将我的网站从 ASP.NET MVC3 (Razor) 升级到了 MVC4 (Razor2),在这样做的过程中,我发现 Razor 视图引擎似乎发生了重大变化.

场景(大大简化)如下所示.

@model IEnumerable@{ 布局 = 空;}<!DOCTYPE html><身体><div>@foreach(模型中的 var x){@string.Format("Foo bar: {0}", x)//MVC4/Razor2 中的错误}

这在 MVC3/Razor 中工作正常,但是在 MVC4/Razor2 中,string.Format 行导致错误:

<块引用>

@"字符后出现意外的字符串"关键字.一旦进入代码,您就不需要在诸如字符串"之类的结构前面加上@".

如果删除 @,视图引擎会要求您用分号终止 string.Format 行.然而,ReSharper 随后发出警告(正确地如此):

<块引用>

未使用纯方法的返回值.

我为此找到的两个修复方法是使用 :

@string.Format("The value {0}", x)</text>

或者使用 @:@ 的更奇特的方法:

@:@string.Format("数值{0}", x)

这是 Razor 视图引擎中已知并记录在案的更改吗?

解决方案

似乎是一个错误.它适用于 String:

@foreach(模型中的 var x){@String.Format("Foo bar: {0}", x)}

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>

This works fine in MVC3/Razor, however in MVC4/Razor2 the string.Format line results in an error of:

Unexpected "string" keyword after "@" character. Once inside code, you do not need to prefix constructs like "string" with "@".

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.

The two fixes I've found for this are either to use <text>:

<text>@string.Format("The value {0}", x)</text>

Or a more curious approach using @:@:

@:@string.Format("The value {0}", x)

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 Razor 中可能的重大更改,可以通过“@:@"修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆