In the documentation of ASP.NET MVC says that you should do something like this when have a link in a view
<a href="@href("~/SubPage")">Subpage</a>.
The razor engine replaces @href("~/SubPage")
to /Subpage
.
What is the advantage of do it this way instead
<a href="/SubPage">Subpage</a>.
In cases like this and in others (like creating a form) why use the razor engine instead of write directly what you want. I think is faster on the server side to print something directly that let the engine to generate it.
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.
Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.
Razor Expression Encoding Razor provides expression encoding to avoid malicious code and security risks. In case, if user enters a malicious script as input, razor engine encode the script and render as HTML output.
Razor View Engine is a markup syntax which helps us to write HTML and server-side code in web pages using C# or VB.Net. It is server-side markup language however it is not at all a programming language.
If your application runs in a subfolder, the Razor @href will create the correct link like this:
www.myapp.com/subfolder/SubPage
If you write it by yourself your link will be like this and will not work:
www.myapp.com/SubPage
Thats because ~
will be replaced with your application root by Razor.
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