Related Tags:
C# C# (pronounced "C-sharp") is an object-oriented programming language from Microsoft that aims to combine the computing power of C++ with the programming ease of Visual Basic. C# is based on C++ and contains features similar to those of Java. Learn More, HTML Hypertext Markup Language, a standardized system for tagging text files to achieve font, colour, graphic, and hyperlink effects on World Wide Web pages. Learn More, ASP.NET MVC The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly. Learn More, MVC 4 It is a newer version of ASP.NET MVC,ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. Learn More, MVC View View is a user interface. View displays data from the model to the user and also enables them to modify the data. ASP.NET MVC views are stored in Views folder. Learn More,

how to display/render "@" symbol as string in mvc view as razor syntax uses the @ symbol to transition from HTML to C#?

Description:I am trying to write following code in my MVC View.


<script type="application/ld+json">
{
"@context" : "http://mywebsite.com",
"@type" : "WebSite"
}
</script>

but it is throwing error "The name 'context' does not exist in the current context" as it consider it as C# code.

Posted by: | Posted on: Jun 27, 2019

1 answers

Replies

6

There are few ways to achieve this in MVC View.
1) by using double @@ in this case you can write your code as follows:

<script type="application/ld+json">
{
"@@context" : "http://mywebsite.com",
"@@type" : "WebSite"
}
</script>

2)by using @Html.Raw method in this case you can write your code as follows:

<script type="application/ld+json">
{
"@Html.Raw("@context")" : "http://mywebsite.com",
"@Html.Raw("@type")" : "WebSite"
}
</script>

3)By using @() to simply write string inside the brackets ,in this case your code will become as:

<script type="application/ld+json">
{
"@("@context")" : "http://mywebsite.com",
"@("@type")" : "WebSite"
}
</script>

All the above three will do the trick for you.

Replied by: | Replied on: Jun 27, 2019



Reply
×

Code block Hyperlink bold Quotes block Upload Images

Preview