Home How to use properly razor syntax inside script block?

# How to use properly razor syntax inside script block?

Michael
1#
Michael Published in 2017-11-13 19:55:39Z
 I work on an ASP.NET MVC 5 project. I try to use razor syntax in script area on the cshtml page:   But when I run the page I get on this row: url = '@Url.Content("~/Account/GetLayers/")';  This error: CS1012: Too many characters in character literal So I tried this:  url = "@Url.Content("~/Account/GetLayers/")";  But now I get this error: CS1002: ; expected Any idea why my attempts above don't work?
Shyju
2#
 Because you are already in a C# code block, (opened by your if condition statement). If you want to mix js/plain text with C# code in razor, use @: prefix If you are trying to generate the relative url to an action method, you should consider using Url.Action method.   The @: tells razor that the following expression should be considered as not code, but plain text It is same as using the  tag @if (!Request.IsAuthenticated) { url = '@Url.Action("Index")'; }  You can convert this to a one liner with ternary operator