What is the difference between Pages and Shared and can we use different folders?

Viewed 253

I am browsing and still learning about Blazor, but one thing I am unable to understand - why are there prepared folders for "Pages" and "Shared", and what's the difference between the two?

  • What is the difference between Pages and Shared?
  • Where do we stored our custom controls?

solution explorer view

1 Answers

Pages and components are essentially the same thing, except a page has one or more @page directives identifying which URL can be used to render them.

A @page directive will be turned into a [Microsoft.AspNetCore.Components.RouteAttribute("/XXX")] attribute on the component class that is generated from your razor file.

Blazor will reflect over your whole assembly for types that are decorated with [RouteAttribute]. This means pages can be wherever you want them to be.

The same is true for Components. The reason are able to put them in the Shared folder by default is because /_Imports.razor has a @using YouAppName.Shared in it. If you want to put your components in other namespaces you can either

  1. Add the namespace to the _Imports.razor file
  2. Add a @using WhateverNamespace to the top of your consuming page/component.
  3. Include the namespace in the markup <WhateverNamespace.SomeComponent/>

https://blazor-university.com/routing/defining-routes/

Related