asp.net-corerazor-components

Razor components vs View Components


I recently upgraded from Asp .NET Core 2.2 to 3.1 and can use Razor Components in razor pages. They look basically like View Components except it uses blazor and everything on the same file. I tried to search but i haven't read anything on documentation that says to use Razor Components instead View Components.

So here is the question...

What is the point of using Razor Components instead View Components in Razor Pages? Is it better? I know View Components are a bit painful to set because you have to set a default view and a code behind file with the logic and you cannot use javascript on it, only on the parent view. Most of my apps are full of View Components and I was wondering what are the advantages of switching to razor components?


Solution

  • While View Components are more like partial views but asynchronous, DI friendly, and kind of independent. They are mostly for view-only purposes (yeah it's possible to add some update logic via AJAX but it's more like a hack rather than a feature) and are used mostly to re-use common UI units like navigation, login block, sidebar, etc.

    Razor Components are sort of "full featured" UI components, which can be easily nested, pass parameters through the components hierarchy, raise and handle UI events, and so on. So you can easily use them to manage your app UI, or even create custom UI component libraries.