I create a String
in a view and want to output it to the screen.
Initially I tried Response.Write
but, due to reasons explained elsewhere on this site, the content appeared at the top of the page. I then attempted to output the string using @
like so: @myString
.
This worked, in that it output the text at the right location but it escaped HTML links that I had put in there. How do I get around this problem?
The difference between them is that View Pages are Razor views that are used to provide the HTML representations (aka views) for services in much the same way View Pages work for MVC Controllers.
From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.
To declare a variable in the View using Razor syntax, we need to first create a code block by using @{ and } and then we can use the same syntax we use in the C#. In the above code, notice that we have created the Code block and then start writing C# syntax to declare and assign the variables.
Razor is a powerful templating implementation that's very useful to generate text and HTML output for all sorts of purposes. As you've seen in this article, you can use ASP.NET MVC Razor views from just about anywhere in your ASP.NET applications fairly easily with the Helper class provided here.
You can use Html.Raw()
:
@Html.Raw(MyStringVar)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With