How does Blazor Server route
There isnt a ton of documentation on the Blazor server app, and I know it uses SignalR, but the project does not contain a HomeController. So I am wondering how is routing down in blazor without it? Is it just base on Role/Policy authorization for pages and components?
Your question is not very specific, but the routing in a blazor app is done by Router
component in the App.razor
. The Router
will scan the given Assembly for razor-components that have the @page
directive. These razor-components are routable and will be able to get displayed by the RouteView
-component.
App.razor:
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<p>Sorry, there's nothing at this address.</p>
</NotFound>
</Router>
For authorization there is another RouteView
-component called AuthorizeRouteView
, but this requires that you wrap the whole Route
-component with a CascadingAuthenticationState
.
App.razor with auth:
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData"
DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
Basic routing is pretty well described in this Microsoft docu here. For auth with routing there is also a docu article here.