简体   繁体   中英

Weird ASP.NET Razor page error in Javascript For loop: Name cannot begin with the character

Web page displays an ASP.NET error like below:

在此处输入图像描述

The line is pure JS code. I could track down the exact line in for loop:

在此处输入图像描述

I have no idea what's wrong with JS code in Razor page. If I remove for loop, the error disappears. How can I fix this?

I found the reason; Razor engine encodes the < and > characters so the browser throws JS compile error:

在此处输入图像描述

The solution is simple:

1- Wrapping inside the < script> < /script> tags with <! -- -->

2- Using @Html.Raw("<") instead of <

<script>
<!--  

...
// i < len;  // Replace this line as below
i @Html.Raw("<") len;
...
-->
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM