Search code examples
c#razorblazorblazor-server-side.net-core-3.0

Why wont Blazor route to component in razor library?


I have a Blazor app and a Razor library.

In my Razor library, I have a component, AccountNavigation.razor that i am able to use with html syntax and it works correctly, like so: <AccountNavigation />

The problem is with another component, Login.razor is in the same library, with @page "/login" written at the top of it. No links work to href="/login" or even if i try the route manually it does not work. If I move Login.razor to the Blazor app project, it then will work.

My Razor library project is as follows:

<Project Sdk="Microsoft.NET.Sdk.Razor">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <Version>1.0.3.5</Version>
    <LangVersion>8.0</LangVersion>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>


  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Components" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.RazorPages" Version="2.2.5" />
  </ItemGroup>

</Project>

I've also tried targetting net core 3.0 and same issue. My Razor library only has 3 files in it. The working AccountNavigation.razor, Login.razor, and _Imports.razor. Is there something that I am missing?


Solution

  • Thanks to CoolBots for pointing me to the documentation on Blazor Routing, I required this crucial part:

    Use the AdditionalAssemblies parameter to specify additional assemblies for the Router component to consider when searching for routable components. Specified assemblies are considered in addition to the AppAssembly-specified assembly. In the following example, Component1 is a routable component defined in a referenced class library. The following AdditionalAssemblies example results in routing support for Component1:

    <Router AppAssembly="typeof(Program).Assembly"
    AdditionalAssemblies="new[] { typeof(Component1).Assembly }> ...