ASP.NET Core MVC middleware will find and execute the Razor template (.cshtml file) when the Controller returns ViewResult. Razor templates use syntax that combine C# and HTML to produce the final HTML page.
Problem
How does ASP.NET Core MVC uses Razor engine to create dynamic Views.
Solution
Configure conventional routing, as described in previous post, and add following Controller.
Add the two Views (Index and Bio) in Views folder
See discussion section to go through Razor syntax.
ASP.NET Core MVC middleware will find and execute the Razor template (.cshtml file) when the Controller returns ViewResult. Razor templates use syntax that combine C# and HTML to produce the final HTML page.
When ViewResult executes, it searches for the View using the paths (in this sequence).
If template filename is different than action name, then Controller can return ViewResult and specify this.
HTML is rendered unchanged.
Transition from HTML to C# is via @ symbol. C# code blocks are used with @ { // code },
C# expressions are used with @ symbol,
or @( // expression ),
C# expressions are HTML encoded, below is HTML output and how browser renders it,
@Html.Raw can be used to avoid encoding, below is HTML output and how browser renders it,
You could use various control structures in as a code block too, e.g. @if, @switch, @for, @foreach, @while, @do while and @try. Below are examples for these,
Razor Views are converted into C# classes (behind the scenes) that inherit from RazorPage. Directives are ways to change the behavior of these classes or the View Engine. The commonly used directives are,
@using
Adds a using directive to generated C# class. Like C#, it’s used to import namespaces.
@model
Type specified will be used as T in RazorPage. Model is supplied from the controller when returning ViewResult. This type can then be access via Model property of the page.
@inject
Used to inject services (registered in service container in Startup). You need to provide the service type and a name (to be used in the view). Dependency injection is views is useful when supplying strongly typed lookup data to views, which otherwise needs dynamic ViewData or ViewBag.
Below is an example of @using, @model and @inject directives.
Create a service
Configure service in ASP.NET Core service container
Create a Model.
Return Model from Controller action.
Now, you can use the model and service from the View.
Source Code
GitHub
View All
View All