MVC3到MVC4 RTM手动升级问题:@这个关键字抛出错误的前面 [英] MVC3 to MVC4 RTM Manual Upgrade issue: @ in front of this keyword throwing error

查看:216
本文介绍了MVC3到MVC4 RTM手动升级问题:@这个关键字抛出错误的前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有了code的下面的行工作就好了一个MVC3项目:

  @if(this.Model.ShowAddButton)
{
    @ this.Html.ActionLink(添加,添加)
}

我们的团队有一个编码准​​则,所有本地方法调用需要与 pfixed $ P $本。这曾在MVC3就好了,直到现在。

我手动升级的项目MVC4,利用莅临指导。现在,code以上以下消息的错误了:

 意外这个关键字后@字符。一旦code里面,你不需要preFIX结构,如本与@。

我觉得错误消息是误导。它意味着它是合法的做到这一点,但不是必要的。我故意这样做,即使我知道它是不是需要遵守编码规则。解析器失败的事实,该消息应指示一旦code里面,你不能preFIX .... 我明白你不能使用 @ 嵌套code块,这个问题是不是与 @ 迹象,但使用这个。添加这个来语句不影响通话的结果,所以我不明白为什么它抛出异常。我可以通过删除修复该问题这个

  @if(this.Model.ShowAddButton)
{
    @ Html.ActionLink(添加,添加)
}

但这样做会与我们的编码准则相冲突。所以我的问题是,这是不是在MVC4是故意改变(因为它在MVC3工作只是罚款)。或者这是MVC4错误?如果我删除如果块,我仍然可以使用这个关键字。

  @ this.Html.ActionLink(添加,添加)


解决方案

这其实是一个错误中MVC4。一个报告被提出。

这两种变通是:

  @if(this.Model.ShowAddButton)
{
    @ Html.ActionLink(添加,添加)
}

  @if(this.Model.ShowAddButton)
{
    @(this.Html.ActionLink(添加,添加))
}

I have a MVC3 project that has the following line of code which worked just fine:

@if (this.Model.ShowAddButton)
{ 
    @this.Html.ActionLink("Add", "Add")
}

Our team has a coding guideline that all local method calls need to be prefixed with this. This has worked just fine in MVC3 up until now.

I've manually upgraded the project to MVC4, using the guidance from here. Now the code above errors out with the following message:

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

I think the error message is misleading. It implies that it is legal to do this, but not necessary. I am purposely doing this even though I know it is not needed to adhere to coding guidelines. The fact the parser fails, the message should indicate Once inside code, you cannot prefix.... I understand you can't use @ in nested code blocks, and the issue isn't with the @ sign, but the use of this. Adding this to the statement doesn't affect the result of the call, so I don't understand why it is throwing an exception. I can fix the issue by removing this:

@if (this.Model.ShowAddButton)
{ 
    @Html.ActionLink("Add", "Add")
}

But doing this will conflict with our coding guidelines. So my question is, is this something that was purposely changed in MVC4 (since it worked just fine in MVC3). Or is this a bug in MVC4? If I remove the if block, I can still use the this keyword.

@this.Html.ActionLink("Add", "Add")

解决方案

This is in fact a bug in MVC4. A report was filed.

The two work arounds are:

@if (this.Model.ShowAddButton)
{ 
    @Html.ActionLink("Add", "Add")
}

OR

@if (this.Model.ShowAddButton)
{ 
    @(this.Html.ActionLink("Add", "Add"))
}

这篇关于MVC3到MVC4 RTM手动升级问题:@这个关键字抛出错误的前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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