24. Using View Components Flashcards

1
Q

What are view components?

A

View components are classes that provide application logic to support partial views or to inject small fragments of HTML or JSON data into a parent view.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why are view components useful?

A

Without view components, it is hard to create embedded functionality such as shopping baskets or login panels in a way that is easy to maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are view components used?

A
View components are typically derived from the ViewComponent class and are applied in a parent view using the custom vc HTML element or the @await Component.InvokeAsync() expression.
Using the name of the view component class as the argument.
View components can be used only in controller views or Razor Pages and cannot be used to handle requests directly.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Are there any pitfalls or limitations with view components?

A

View components are a simple and predictable feature. The main pitfall is not using them and trying to include application logic within views where it is difficult to test and maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Are there any alternatives to view components?

A

You could put the data access and processing logic directly in a partial view, but the result is difficult to work with and hard to maintain.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How to apply View Components Using a Tag Helper?

A

Razor views and pages can contain tag helpers, which are custom HTML elements that are managed by C# classes.
View components can be applied using an HTML element that is view components can be applied using an HTML element that is.
The tag for the custom element is vc, followed by a colon, followed by the name of the view component class, which is transformed into kebab-case. Each capitalized word in the class name is converted to lowercase and separated by a hyphen so that CitySummary becomes city-summary, and the CitySummary view component is applied using the vc:city-summary element.
So you can write

with tag helper, instead of
@await Component.InvokeAsync(“CitySummary”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to apply View Components in Razor Pages

A

Razor Pages use view components in the same way, either through the Component property or through the custom HTML element.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain ViewViewComponentResult

A

This class is used to specify a Razor view, with optional view model data. Instances of this class are created using the View method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Where are view components stored?

A

Notice that view components used in Razor Pages will find views defined in the Views/Shared/Components folder and that view components defined in controllers will find views in the Pages/Shared/Components folder. This means you don’t have to duplicate views when a view component is used by controllers and Razor Pages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is ContentViewComponentResult class used for?

A

This class is used to specify a text result that will be safely encoded for inclusion in an HTML document. Instances of this class are created using the Content method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what is HtmlContentViewComponentResult class used for?

A

This class is used to specify a fragment of HTML that will be included in the HTML document without further encoding. There is no ViewComponent method to create this type of result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When are asynchronous view components useful?

A

Asynchronous view components are useful when there are several different regions of content to be created, each of which can be performed concurrently. The response isn’t sent to the browser until all the content is ready. If you want to update the content presented to the user dynamically, then you can use Blazor, as described in Part 4.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly