CS1525 Razor 视图页面 ASP.NET 中的无效表达式术语“>”-核心 2.2

CS1525 Invalid expression term '>' in razor view page ASP.NET-core 2.2

我的 ASP.NET-Core 2.2 MVC 应用程序的 Razor 页面视图中有以下代码

<p> 
   @{ 
      if(course.Description.Length > 100)
      {
       @course.Description.Substring(0, 100)  @:"..."
      }
      else
      {
      @course.Description;
      }
    }
 </p>

但它给了我以下错误

Error CS1525 Invalid expression term '>'

我不知道如何解决这个问题。如果可以,请帮助我。

谢谢

我认为您的 if 语句中的 "course" 缺少“@”。 尝试:

<p> 
      @{ 
      if(@course.Description.Length > 100)
      {
       @course.Description.Substring(0, 100)  @:"..."
      }
      else
      {
      @course.Description;
      }
    }
 </p>

你的代码对我来说工作正常。

不过,这里有一种更简洁的写法。看看它是否适合你:

<p>
    @if (course.Description.Length > 100)
    {
        @course.Description.Substring(0, 100) @:"..."
    }
    else
    {
        @course.Description
    }
</p>

如果您仍然遇到问题,可能是您的 Razor 页面中的周围代码有其他问题,或者您的项目没有引用正确的程序集?在新的空白 ASP.NET 项目中尝试 运行 相同的代码。

有趣的是这个@编译器无法决定何时使用异常,例如 如果我这样使用它

@functions{
    public IHtmlContent RenderSubButton(ButtonModel button)
    {
        var @appIcn = "blabla";

        return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"
           + button.Text
           + @appIcn + "</a>");
        }
}

appIcon 将不起作用,但如果我使用

@functions{
    public IHtmlContent RenderSubButton(ButtonModel button)
    {
        var @appIcn = "blabla";

        return @Html.Raw(@"<a href='@Url.Action(" + button.Action + ", " + button.Controller + ", " + button.RouteValues + ")' class='" + button.Class + "' " + button.Binding.ToDataAttributes() + ">"
           + button.Text
           + appIcn + "</a>");
        }
}

然后就可以了。不同的是appIcn头上有@this,没必要