0

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.

Tim
  • 5,435
  • 7
  • 42
  • 62
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • What are you expecting that code to do? As it currently stands, the braces `{ ... }` are just creating a code block, not an object. The lines within them make no sense as it currently stands. Are you trying to create an object? If so, try `var objName = { ... };` You can then use`objName` for whatever your code will do next. – Tim Jul 01 '20 at 11:50
  • Thank you Tim . No it is not an object , I followed this link https://help.woorank.com/hc/en-us/articles/360000136385-Why-can-t-you-find-my-social-media-pages- and it said that I should use these codes for attaching my social media to my webpages . @Tim – neda Derakhshesh Jul 01 '20 at 12:22
  • @Tim The problem is for sure using `@` before context. But I do not know what edit should I do for this – neda Derakhshesh Jul 01 '20 at 12:27
  • That page looks specific to people using woorank - are you using it? If so, it would be a good idea to edit your question to add more detail about what you're trying to do, as the question currently makes no reference to woorank. I don't know anything about woorank, so I'm afraid I won't be able to help. If you're not using woorank then I don't think that page will help you at all. – Tim Jul 01 '20 at 13:38
  • It is not relating to woorank or their users, I just followed the guide to help search engine understand my social media links . Is there any way I can attach my social media links to the views.cshtml? – neda Derakhshesh Jul 01 '20 at 14:08
  • https://thomas.vanhoutte.be/miniblog/schema-org-social-media-websites/ Here is another example of what I need. It introduce another block of codes that may help @Tim – neda Derakhshesh Jul 01 '20 at 14:17
  • If you're just trying to get that block into the HTML which is sent to the client, you will need to escape the `@` symbol. See https://stackoverflow.com/questions/3626250/escape-character-in-razor-view-engine - does that help you? – Tim Jul 01 '20 at 14:23

1 Answers1

1

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.

Tim
  • 5,435
  • 7
  • 42
  • 62
  • Thank you very much, and also this link has another solution that really helps https://thomas.vanhoutte.be/miniblog/schema-org-social-media-websites/ – neda Derakhshesh Jul 01 '20 at 14:35