From the course: Learning ASP.NET Core: MVC, Razor Pages, Web APIs & Other Foundations

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Using Razor syntax

Using Razor syntax

- [Instructor] Razor syntax allows you to embed entire C# code blocks in your pages, as well as simple C# expressions that will be evaluated and rendered as HTML before being sent back to the browser. This default index page we looked at in the last movie already has a C# code block in it just after the page and Model directives. On line three, you can see the @ sign followed by a pair of curly braces. That's a C# code block. I can write just about any C# code I want inside those curly braces. Currently, it contains just one line of code that assigns a value to an element in the special ViewData array. ViewData is a property that exists on all views such as this razor page. It's a simple weekly typed dictionary that is a convenient place to store small bits of data you might need on your page. You access values in the dictionary using string keys. This line of code assigns the string "Home page" to the key named "Title." Let's imagine I want to store a welcome message in the ViewData…

Contents