5
votes

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?

3
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?CoolBots
I presume this component works properly if the Razor library is referenced from an ASP.NET MVC project, right?CoolBots
@CoolBots 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.maksymiuk
Should work then.. Just in case, double check your setup against the docs: docs.microsoft.com/en-us/aspnet/core/blazor/…CoolBots

3 Answers

7
votes

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 }> ...
5
votes

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

2
votes

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