Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 518
maksymiuk Avatar asked Sep 29 '19 04:09

maksymiuk


3 Answers

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 }> ...
like image 187
maksymiuk Avatar answered Oct 06 '22 01:10

maksymiuk


Not sure when or why this started happening. But in my case every time I added or renamed a razor component, it added a <remove>item to that component in the csproj file. Just remove it.

Using asp.net core 3.1, and blazor 3.2 preview 2

like image 30
Morten_564834 Avatar answered Oct 06 '22 00:10

Morten_564834


@maksymiuk is absolutely correct on this one. Change the Router tag in App.Razor file in Core 3.1 and the external routings are included.

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

Funny to note though, as soon as you include 1 component from the Razor library you will find all the other routings in other components (at least in the same Areas/Pages folder) will also work.

like image 45
Marijn Pessers Avatar answered Oct 06 '22 00:10

Marijn Pessers