Search code examples
asp.net-coreroutesblazorblazor-server-siderazor-components

Blazor (Server-Side) RouteAttribute is broken in core 3.0 release?


I migrated from preview 7 to release Core 3.0 i have a library with only-class components i have this LocationViewModel:ComponentBase it was working like a charm but not anymore :(

My ViewModel:

[Layout(typeof(LayoutComponent))]
[Route("/location")]
public class LocationViewModel : ViewModelBase<Location>
{}

My Layout:

public class LayoutComponent : LayoutComponentBase
{
    ...
    Builder.AddContent(1,Body);
    ...
}

My _Host.cshtml

<app>@(await Html.RenderComponentAsync<LayoutComponent>(RenderMode.ServerPrerendered))
</app>

My app.razor

@using SystemCore.LayoutComponents;
@using Microsoft.AspNetCore.Components.Web;
@using FacilityManagementSystem.ViewModels;

<Router 
        AppAssembly="@typeof(Startup).Assembly" <-- i tried 'App' before , still no use
        AdditionalAssemblies="new List<System.Reflection.Assembly>() { typeof(LocationViewModel).Assembly }"
        >       
    <Found Context="routeData">
        <RouteView RouteData="@routeData" DefaultLayout="@typeof(LayoutComponent)" />
    </Found>
    <NotFound>
        <LayoutView Layout="@typeof(LayoutComponent)">
            <p>Sorry, there's nothing at this address.</p>
        </LayoutView>
    </NotFound>
</Router>

what happens is that my LocationViewModel does not render when i go to location '~base/location'

i tried this to check whether my view model has something wrong with it..

_Host.cshtml

<app>@(await Html.RenderComponentAsync<LocationViewModel>(RenderMode.ServerPrerendered))
</app>

AND IT WORKED !

my assumption is that something is not catching my view model when not rendered directly please help as this is so critical to me, sorry for bothering

Edit (1) : there is no error appearing in the browser console BTW


Solution

  • i solved the problem to the issue ! in-order to use your own custom LayoutComponent you have to remove the DefaultLayout=@MainLayout from App.razor otherwise renderer will force MainLayout on each component even if it has LayoutAttribute with type other than MainLayout

    Hope it helps...