Last updated .
< Previous chapterNext chapter >
MVC views are constructed as Razor views. The result is a file containing html interspersed with Razor statements and expressions. The html need not be a complete html document, it may also be a HTML fragment. Razor view files are recognized by their .cshtml extension. The file is not compiled it is needed at runtime. This has the downside that you will sometimes not catch errors until you run the application, but has the benefit that you can edit the view file and see immediate results. There is no need to shut down the app, correct the error and start it again. You can just fix the error in the view file and then hit F5 in the browser to see the result of the change.
Building on the sample application from the last chapter, I have deliberately made a typo in the index.cshtml file (the startup page), typing "Shame" instead of "Name":
Note that the code editor clearly shows there is a problem, as indicated by the red wavy line. Nevertheless, you can start debugging the app by presssing F5 with no problem.
If you do that, you'll just see a blank page in the browser. The problem is that, by default, ASP.Net will not write error information to the response. To enable that while developing
(but not for a production environment), we need to add this configuration to the
Startup.cs (partial)
.....
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
.....
This configuration makes ASP.Net output some exception information to the response. When you add this code, you'll need to install a package, as shown in the pop-up message:
Just click on the yellow bar reading "Add package Microsoft..." to add the required package.
Now, the browser view clearly tells you of the problem in the Razor view file:
A Razor expression is recognized by the starting "@" character. This character denotes that .Net code is what follows, not markup. As an example consider the code
<title>@ViewBag.Title</title>
.
It starts with an HTML tag, then a Razor statement and then a closing HTML tag. Notice there is no explicit termination of the Razor statement. The Razor engine is usually
smart enough to work out that some text is not code (in this example </title>
)
and so implicitly terminates the statement. Sometimes, though, the parser needs a little help to find out which is which. In that case, you can disambiguate things by wrapping the entire
code block in parentheses. For example, the previous statement can be rewritten as <title>@(ViewBag.Title)</title>
.
Notice in the above examples, there are no semicolons to terminate the code statement. So what if you need more than one line of code in a given place? Use a code block. A code block is is recognized by wrapping curly parentheses, as in this example:
@{
string message = "Time is ";
message += DateTime.Now.ToString();
}
<p>@message</p>
The above example also shows how to declare and use variables in Razor. The variable is then in scope throughout the file.
Loops are constructed using the well-known constructs, for example:
@for (int x = 0; x < 3; x++) {
<li>@x</li>
}
Note that the curly braces are mandatory. Aften the opening brace, the context switches from code to markup.
Again, conditional statements follow the same pattern as for iteration:
@{ bool condition = true; }
@if (condition) {
<p>Yes!</p>
}
The result of an expression is automatically html-encoded. An example:
@{
bool condition = true;
string message = "<p>Hello</p>";
}
@if (condition) {
<p>@message</p>
}
When shown in the browser, the output from the above example reads "<p>Hello</p>". After the opening brace, Razor expects an HTML tag. If you want to render some literal (and unencoded) text instead, you can use this syntax:
@{
bool condition = true;
}
@if (condition) {
@:Hello from here!
}
If you need to display the @ character as part of some literal text, you escape it by doubling it, for example:
<p>@@ is the 'at' character</p>
Note, however, that Razor recognizes email addresses, so the @ needs no escaping in that case:
<p>My email is foo@bar.com</p>
In relation to attributes, where a Razor expression is used to generate the value of the attribute, there is some special behaviour. If the expression evaluates to null, the attribute is not rendered:
@{
string url = null;
}
<a href="@url">My link</a>
In the above example, the anchor tag has no resulting
For those HTML attributes that really are booleans, Razor omits the attribute if the expression evaluates to null. For example:
@{
bool isChecked = false;
}
<input type="checkbox" checked="@isChecked" />
The above example results in the markup <input type="checkbox" />
for the checkbox. Had the expression evaluated to true, the markup would have been <input type="checkbox" checked="checked" />
.
A strongly typed view is created when a
@model Contact[]
<ul>
@foreach (Contact contact in Model) {
<li>@contact.Name: @contact.Email</li>
}
</ul>
<p>@Model.Count() contacts</p>
Namespaces can be brought into scope with a
@using MVCTutorial.Models
< Previous chapterNext : The MVC URL Routing >
2016 by Niels Hede Pedersen