Razor is a markup syntax for embedding .NET based code into webpages. It includes Razor markup, C#, and HTML. Files containing Razor generally have a .cshtml file extension. Razor is also found in Razor component files (.razor).
Razor is designed to be lightweight and unobtrusive, so you focus on your markup. However, there are a lot of hidden features that really come in handy! I asked some friends on the .NET team for some of their favorite Razor features, here's what they had to say.
Tag Helpers enable server-side code to participate in creating and rendering HTML elements in Razor files. There are a lot of built-in ones, but it's also very easy to write your own.
Here's an example of an asp-if Tag Helper that suppresses the output of the element if the supplied predicate equates to false.
[HtmlTargetElement("*",Attributes="asp-if")]publicclassIfTagHelper:TagHelper{/// <summary>/// The predicate expression to test./// </summary>[HtmlAttributeName("asp-if")]publicboolPredicate{get;set;}publicoverridevoidProcess(TagHelperContextcontext,TagHelperOutputoutput){if(!Predicate){output.SuppressOutput();}}}
Here's how you'd use it:
<divasp-if="(DateTime.UtcNow.Minute % 2) == 0">
This paragraph will only render during <strong>even</strong> minutes.
</div><divasp-if="(DateTime.UtcNow.Minute % 2) == 1">
This paragraph will only render during <strong>odd</strong> minutes.
</div>
Templated components are pretty useful. They're components that accept one or more UI templates as parameters, which can then be used as part of the component's rendering logic. Templated components allow you to author higher-level components that are more reusable than what was possible before. For example, a list view component could allow the user to specify a template for rending items in the list, or a grid component could allow the user to specify templates for the grid header and for each row.
I like Razor Component Libraries. Just write a .razor file and it's a component that can be referenced. I used these a lot in my BlazorWebFormsComponents project, which helps with migrating ASP.NET Web Forms project to Blazor.
Here's an example Label component, implemented as Label.razor:
We believe that Web Forms applications that have been well maintained and provide value should have a path forward to the new user-interface frameworks with minimal changes. This is not an application converted nor is it a patch that can be applied to your project that magically makes it work with ASP.NET Core. This repository contains a library and series of strategies that will allow you to re-use much of your markup, much of your business code and help shorten your application re-write process.
This is not for everyone, not everyone needs to migrate their application. They can continue being supported as Web Forms for a very long time (2029 EOL at the time of this writing) and the applications that are considered for migration…
Here's a really useful Razor snippet. This is one of the things I love about Razor syntax, not only do I get to use my existing C# skills but I can use all the language features that make me a productive developer.
Your eyes are not fooling you, that is in fact a using static declaration — meaning that all of the static members of the System.Runtime.InteropServices.RuntimeInformation type are available without their type qualifier. I can seamlessly mesh HTML and C# together, using a powerful templating engine to create rich-content. This is a simple example, but there are many more. You’re limited by your imagination and the constructs surrounding HTML and C# semantics. When this is rendered as HTML, you’d end up with a <div> element containing the .NET framework description. I love seeing the runtime version in my .NET apps 💜.
In most cases, Razor implicitly transitions between C# and HTML without you needing to think about it. It's occasionally useful to explicitly call out a block as HTML text inline in C# code. You can do that with an explicit transition. For example, this can be useful if you're writing out JavaScript or CSS from within a C# code block.
You can use the <text></text> tag structure to delimit a block, and you can use @: for a single line.