Why wont Blazor route to component in razor library?
Asked Answered
P

5

12

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?

Perutz answered 29/9, 2019 at 4:26 Comment(4)
Can you describe what happens when it doesn't work? What's the error? Also, if i you add another component to the same Razor library (Test.razor), does that component work?Burnette
I presume this component works properly if the Razor library is referenced from an ASP.NET MVC project, right?Burnette
@Burnette there's no error, I wish there was so I could diagnose something, but instead it goes to the not found view, which by default just says Sorry, there's nothing at this address. And yes, the library is referenced correctly, and another component in the Razor library works when i use it as a view component as stated in my original question. The problem only arises when I try and use the components as pages and try routing to them.Perutz
Should work then.. Just in case, double check your setup against the docs: learn.microsoft.com/en-us/aspnet/core/blazor/…Burnette
P
24

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 }> ...
Perutz answered 29/9, 2019 at 5:10 Comment(0)
S
17

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

Sadism answered 15/3, 2020 at 8:16 Comment(4)
Crazy enough this was my problem.. spending 3 hours wondering why only one .razor page works.. any new ones were blank! No error, no nothing!. Gotta love using stuff thats so newRacehorse
But why Microsoft why?Conference
This happened to me in the latest version of BlazorWrier
I've seen people blame this on ABCPDF or another package causing the issueWendell
D
6

@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.

Dyna answered 3/5, 2020 at 11:38 Comment(2)
"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" That's because you don't include the component. You include the assembly to be added to the list of assemblies that are "scanned" for classes decorated with the page route attribute.Kab
True. To me it feels there should be a better way to get an assembly rather than taking the assembly that a certain class belongs to. If I were to refactor that class into another assembly half of my routing would fail.Dyna
O
3

I had the same problem. The suggested solutions here did not work for me.

What solved it for me was adding the library assembly to the razorComponentMap in program.cs:

app.MapRazorComponents<App>()
    .AddAdditionalAssemblies(typeof(RazorClassLibrary1.Component1).Assembly) 
Ornery answered 1/3, 2024 at 12:19 Comment(0)
B
1

In case anybody has the same issue:

I accidently deleted the @Body from my MainLayout.razor file so there was no change visible upon routing.

Bloodfin answered 6/11, 2023 at 13:3 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.