Razor/JavaScript 和尾随分号 [英] Razor/JavaScript and trailing semicolon

查看:29
本文介绍了Razor/JavaScript 和尾随分号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Visual Studio 2012,在 Razor 视图页面的 JavaScript 部分,我得到了我认为是 Razor 语法与 JavaScript 语法之间的斗争.特别是,脚本部分的尾随分号被智能感知标记,并提供编译器警告(不是错误):

Using Visual Studio 2012, on a Razor view page, in the JavaScript section, I am getting what I think is a battle between Razor syntax vs JavaScript syntax. In particular, the trailing semicolon in the script section is flagged by intellisense and a compiler warning (not error) is delivered:

'警告 13 语法错误'.

'Warning 13 Syntax error'.

如果我将其删除,则会收到语句终止建议(在这种情况下为 ReSharper,但只是好的做法).

If I remove it, then I get a statement termination recommendation (ReSharper in this case, but just good practice).

<script type="text/javascript">
    $().ready(function(){
        var customer = @Html.Raw(ViewBag.CustomerJSON);  // <- Razor (I think) doesn't like this semicolon
    });
</script>

这是 Razor 中的错误吗?如果是这样,有没有办法重写它来避免这个问题?

Is this a bug in Razor? If so, is there a way I can rewrite this to avoid this issue?

推荐答案

这是 Razor 中的错误吗?

Is this a bug in Razor?

绝对不是.运行您的应用程序,它将按预期工作.

Absolutely not. Run your application, and it will work as expected.

这是您使用的工具(VisualStudio 2012、ReSharper 等)中的一个错误,无法识别完全有效的语法并警告您不应该警告的内容.您可以尝试在 Microsoft Connect 站点上打开一个问题,并在尚未完成的情况下发出此错误信号.

It is a bug in the tools you are using (Visual Studio 2012, ReSharper, ...) that are incapable of recognizing perfectly valid syntax and warning you about something that you shouldn't be warned about. You could try opening an issue on the Microsoft Connect site and signalling this bug if that hasn't already been done.

这篇关于Razor/JavaScript 和尾随分号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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