2

I am trying to add a link to .js file in Pages/_Layout.cshtml However I am getting following exception:

Severity Code Description Project File Line Suppression State Error (active) CS0103 The name 'antv' does not exist in the current context Blazing C:\Users\Laptop\source\repos\Blazing\Blazing\Pages_Layout.cshtml 31

_Layout.cshtml:

@using Microsoft.AspNetCore.Components.Web
@namespace BlazingDiary.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <base href="~/" />
  <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
  <link href="css/site.css" rel="stylesheet" />
  <link href="BlazingDiary.styles.css" rel="stylesheet" />
  <component type="typeof(HeadOutlet)" render-mode="ServerPrerendered" />
</head>
<body>
  @RenderBody()

  <div id="blazor-error-ui">
    <environment include="Staging,Production">
      An error has occurred. This application may no longer respond until reloaded.
    </environment>
    <environment include="Development">
      An unhandled exception has occurred. See browser dev tools for details.
    </environment>
    <a href="" class="reload">Reload</a>
    <a class="dismiss"></a>
  </div>

  <script src="_framework/blazor.server.js"></script>
  <script src="https://unpkg.com/@antv/g2plot@1.1.28/dist/g2plot.js"></script>
  <script src="_content/AntDesign.Charts/ant-design-charts-blazor.js"></script>
  <link href="_content/AntDesign/css/ant-design-blazor.css" rel="stylesheet" />
  <script src="_content/AntDesign/js/ant-design-blazor.js"></script>
</body>
</html>

Problems is in this particular link https://unpkg.com/@antv/g2plot@1.1.28/dist/g2plot.js. Was googling for couple of hours without success. Any ideas?

10101
  • 2,232
  • 3
  • 26
  • 66
  • 1
    The `@` is a special character in razor syntax. You will need to escape it with another `@`. i.e. ` ` – phuzi Nov 27 '21 at 11:24
  • Does this answer your question? [Escape @ character in razor view engine](https://stackoverflow.com/questions/3626250/escape-character-in-razor-view-engine) – phuzi Nov 27 '21 at 11:25
  • @phuzi - but why does it have to be escaped? – H H Nov 27 '21 at 11:26
  • 1
    I thought "The @ is a special character in razor syntax." would have explained it... This is how a `.cshtml` page is interpreted by ASP.Net. Just like `@RenderBody` earlier. Why do you think you don't need to? – phuzi Nov 27 '21 at 11:28
  • @phuzi : You don't have to (shouldn't) escape it in .net6. So it's a bug. – H H Nov 27 '21 at 13:10
  • @HenkHolterman Did I miss something, didn't see where OP said they were using ASP.Net Core 6 – phuzi Nov 29 '21 at 08:19
  • He is definitely not using 6. I interpret the difference between 5 and 6 as a bug fix. I don't think razor should process src attributes in a cshtml file. – H H Nov 29 '21 at 08:43

1 Answers1

4

This is solved in .net 6.

In .net 5 (and before) the razor engine appears to stick its nose where it doesn't belong. Luckily there is an easy fix, escape the @ as @@:

<script src="https://unpkg.com/@@antv/g2plot@1.1.28/dist/g2plot.js"></script>
H H
  • 263,252
  • 30
  • 330
  • 514