首页 > 解决方案 > 在视图中的 Razor 表达式中编码双引号

问题描述

我在 ASP.NET Core Razor 视图上有以下表达式:

<a href="@(Options.CurrentValue.Applications.First(x => x.Name == "Spa").Url)">

当我构建它时,我得到了错误:

A space or line break was encountered after the "@" character.  Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following "@" with no space in between.

我在 @ 之后添加了括号,因为我的代码中有双引号。

如何解决这个问题?

标签: asp.net-corerazor

解决方案


当@ 之后存在空格、制表符(或任何特殊字符)时,我会遇到同样的错误。尝试使用 CTRL + E、S(在 VS 中)修改代码以显示所有特殊字符。

作为变体,您可以用单引号替换双引号:

<a href='@Options.CurrentValue.Applications.First(x => x.Name == "Spa").Url'>

推荐阅读