View components
A view component is like a small controller and view bundled together, used to render a reusable piece of a page that needs its own data, such as a login panel, a menu or a cart summary. This lesson shows how to build and invoke one.
- StartA view invokes it@await Component.InvokeAsync("CartSummary")
- Step 2The class is createdCartSummaryViewComponent gets its services through DI
- Step 3InvokeAsync runsLoads the data, for example the items in the cart
- Step 4It returns View(model)Like an action, but returns IViewComponentResult
- Step 5Its view rendersViews/Shared/Components/CartSummary/Default.cshtml
- EndHTML is insertedThe output appears where it was invoked
The idea in plain words
Some parts of a page appear everywhere but need their own data. A cart summary in the header, for example, must read the cart on every page. Putting that code in every controller is repetitive, and partial views cannot load data. A view component solves this: it is a class with its own logic and a view, invoked from any view or layout.
View components are not reachable by URL, do not use model binding, and run only when a view invokes them.
Example: the class
The class name ends with ViewComponent (or uses the [ViewComponent] attribute). Constructor injection works as it does in controllers.
public class CartSummaryViewComponent : ViewComponent
{
private readonly ICartService _cart;
public CartSummaryViewComponent(ICartService cart) => _cart = cart;
public async Task<IViewComponentResult> InvokeAsync(int maxItems = 3)
{
var items = await _cart.GetItemsAsync();
return View(items.Take(maxItems).ToList());
}
}Example: the view and the call
The default view lives in a folder named after the component. Invoke it with Component.InvokeAsync, or with the vc: Tag Helper once your project assembly is registered with @addTagHelper in _ViewImports.
@* Views/Shared/Components/CartSummary/Default.cshtml *@
@model List<CartItem>
<div class="cart">@Model.Count item(s) in your cart</div>
@* In _Layout.cshtml *@
@await Component.InvokeAsync("CartSummary", new { maxItems = 5 })
@* Or, with @addTagHelper *, MyShop in _ViewImports *@
<vc:cart-summary max-items="5"></vc:cart-summary>Common mistakes
- Putting the view in the wrong folder. It must be under Components/<Name>/ inside the controller views folder or Shared.
- Expecting to reach a view component by URL. Only views can invoke it.
- Doing heavy database work in a component shown on every page without caching.
Interview question
How is a view component different from a controller action?
A view component renders only a part of a page and is invoked from a view, not from an HTTP request, so it has no route and no model binding; its parameters come from the invoking call. It returns IViewComponentResult instead of IActionResult and does not take part in the filter pipeline. Like a controller it supports dependency injection and returns a view with a model.
Want the full details? Read the official Microsoft documentation for this topic.