neda Derakhshesh
neda Derakhshesh

Reputation: 1163

How to use @ in ASp.net MVC views for another purposes not coding with backend

I want to add the social media links to my html. My project is implemented using ASP.net MVC. When I add the below code it gives an error in this line: "@context" : "http://schema.org",

 <script type="application/ld+json">
    {
        "@context" : "http://schema.org",
        "@type" : "Organization",
        "name" : "Example",
        "url" : "https://www.example.com",
        "sameAs" : [
            "https://twitter.com/example",
        ]
    }
</script>

I added this line of code to the <head> tag in _Layout.cshtml. I checked and this code's working fine on a pure html project.

Upvotes: 0

Views: 217

Answers (1)

Tim
Tim

Reputation: 5839

To include that script within the HTML which is served to the client, you need to escape the @ symbol with @@.

<script type="application/ld+json">
    {
        "@@context" : "http://schema.org",
        "@@type" : "Organization",
        "name" : "Example",
        "url" : "https://www.example.com",
        "sameAs" : [
            "https://twitter.com/example"
        ]
    }
</script>

This is because razor uses @ to indicate that some C# code is about to follow (e.g. if you have a C# variable called foo, you would use it within the razor as @foo). With your original code, razor interprets it as using a variable called context and another called type, but it throws an error because it can't find those variables. Using @@ tells razor that you want the string @context rather than the variable context.

Upvotes: 1

Related Questions