Chapter 7. Rendering HTML using Razor views

 

This chapter covers

  • Creating views to display HTML to a user
  • Using C# and the Razor markup syntax to generate HTML dynamically
  • Reusing common code with layouts and partial views

In the previous four chapters, I’ve covered a whole cross-section of MVC, including the MVC design pattern itself, controllers and actions, routing, and binding models. This chapter shows how to create your views—the UI to your application. For desktop apps, that might be WinForms or WPF. For Web APIs, that might be a nonvisual view, such as a JSON or XML response, which is subsequently used to generate HTML in the client’s browser. For traditional web applications, the UI is the HTML, CSS, and JavaScript that’s delivered to the user’s browser directly, and that they interact with.

In ASP.NET Core, views are normally created using the Razor markup syntax (sometimes described as a templating language), which uses a mixture of HTML and C# to generate the final result. This chapter covers some of the features of Razor and how to use it to build the view templates for your application.

Generally speaking, users will have two sorts of interactions with your app: they read data that your app displays, and they send data or commands back to it. The Razor language contains a number of constructs that make it simple to build both types of applications.

7.1. Views: rendering the user interface in MVC

7.2. Creating Razor views

7.3. Creating dynamic web pages with Razor

7.4. Layouts, partial views, and _ViewStart

Summary

sitemap