21. Using Controllers with Views, pt. 1 Flashcards
What is the Razor view engine responsible for?
generating HTML responses that can be displayed directly to the user (as opposed to the JSON and XML responses, which are typically consumed by other applications).
What is a view?
Views are files that contain C# expressions and HTML fragments that are processed by the view engine to generate HTML responses.
The View method creates an instance of the ViewResult class, which implements the IActonResult interface and tells the MVC Framework that a view should be used to produce the response for the client. The argument to the View method is called the view model and provides the view with the data it needs to generate a response.
What are views?
Views are files that contain a mix of static HTML content and C# expressions.
Why are views useful?
Views are used to create HTML responses for HTTP requests. The C# expressions are evaluated and combined with the HTML content to create a response.
How are views used?
The View method defined by the Controller class creates an action response that uses a view.
Are there any pitfalls or limitations to views?
It can take a little time to get used to the syntax of view files and the way they combine code and content.
Are there any alternatives to views?
There are a number of third-party view engines that can be used in ASP.NET Core MVC, but their use is limited.
Why do some files in the Views folder start with underscore?
Files in the Views folder whose names begin with an underscore (the _ character) are not returned to the user, which allows the file name to differentiate between views that you want to render and the files that support them. View imports files and layouts (which I describe shortly) are prefixed with an underscore.
What are directives?
Directives are expressions that give instructions to the Razor view engine. The @model expression is a directive, for example, that tells the view engine to use a specific type for the view model, while the @using directive tells the view engine to import a namespace.
What is content expression? And give examples of content expressions.
Razor content expressions produce content that is included in the output generated by a view.
@
This is the basic Razor expression, which is evaluated, and the result it produces is inserted into the response.
@if
This expression is used to select regions of content based on the result of an expression. See the “Using Conditional Expressions” section for examples.
@switch
This expression is used to select regions of content based on the result of an expression. See the “Using Conditional Expressions” section for examples.
@foreach
This expression generates the same region of content for each element in a sequence. See the “Enumerating Sequences” for examples.
@{ … }
This expression defines a code block. See the “Using Razor Code Blocks” section for an example.
@:
This expression denotes a section of content that is not enclosed in HTML elements. See the “Using Conditional Expressions” section for an example.
@try
This expression is used to catch exceptions.
@await
This expression is used to perform an asynchronous operation, the result of which is inserted into the response. See Chapter 24 for examples.