If I look at the Razor View Engine, then I see a very nice and concise syntax that is not particularly tied to generating html. So I wonder, how easy would it be to use the engine outside asp.net in a "normal" .net environment for example to generate text, code,...
Any pointer, example, comment or explanation is welcome.
aspx pages. By default, Razor Engine prevents XSS attacks(Cross-Site Scripting Attacks) means it encodes the script or html tags like <,> before rendering to view. Razor Engine is little bit slow as compared to Webform Engine. Web Form Engine is faster than Razor Engine.
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.
A Razor Page is almost the same as ASP.NET MVC's view component. It has basically the syntax and functionality same as MVC. The basic difference between Razor pages and MVC is that the model and controller code is also added within the Razor Page itself. You do not need to add code separately.
You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder.
There are two issues here:
<tags>
to determine the transition between code and markup. You can probably use it to generate any text but you might run into issues when your output doesn't match Razor's assumptions about what your intentions are.So for example while this is valid Razor code (because of the <div>
tag):
@if(printHello) { <div>Hello!</div> }
The following snippet is invalid (because the Hello! is still being treated as code):
@if(printHello) { Hello! }
However there's a special <text>
tag that can be used to force a transition for multi-line blocks (the <text>
tag will not be rendered):
@if(printHello) { <text>Hello! Another line</text> }
There is also a shorter syntax to force a single line to transition using @:
:
@if(printHello) { @:Hello! }
Check RazorEngine, it's a little framework built on top of Razor that allows you to do this.
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