简体   繁体   English

为什么 Blazor 不会路由到 razor 库中的组件?

[英]Why wont Blazor route to component in razor library?

I have a Blazor app and a Razor library.我有一个 Blazor 应用程序和一个 Razor 库。

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 />在我的 Razor 库中,我有一个组件AccountNavigation.razor可以与 html 语法一起使用,它可以正常工作,如下所示: <AccountNavigation />

The problem is with another component, Login.razor is in the same library, with @page "/login" written at the top of it.问题在于另一个组件, Login.razor在同一个库中, @page "/login"写在它的顶部。 No links work to href="/login" or even if i try the route manually it does not work.没有指向href="/login"的链接,或者即使我手动尝试路由它也不起作用。 If I move Login.razor to the Blazor app project, it then will work.如果我将Login.razor移动到 Blazor 应用程序项目中,它将起作用。

My Razor library project is as follows:我的 Razor 库项目如下:

<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.我也尝试过针对 net core 3.0 和同样的问题。 My Razor library only has 3 files in it.我的 Razor 库中只有 3 个文件。 The working AccountNavigation.razor , Login.razor , and _Imports.razor .工作AccountNavigation.razorLogin.razor_Imports.razor Is there something that I am missing?有什么我想念的吗?

Thanks to CoolBots for pointing me to the documentation on Blazor Routing , I required this crucial part:感谢 CoolBots 将我指向Blazor Routing的文档,我需要这个关键部分:

Use the AdditionalAssemblies parameter to specify additional assemblies for the Router component to consider when searching for routable components.使用 AdditionalAssemblies 参数指定路由器组件在搜索可路由组件时要考虑的其他程序集。 Specified assemblies are considered in addition to the AppAssembly-specified assembly.除了 AppAssembly 指定的程序集之外,还考虑了指定的程序集。 In the following example, Component1 is a routable component defined in a referenced class library.在以下示例中,Component1 是在引用的 class 库中定义的可路由组件。 The following AdditionalAssemblies example results in routing support for Component1:以下 AdditionalAssemblies 示例导致对 Component1 的路由支持:

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

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.但就我而言,每次我添加或重命名 razor 组件时,它都会在 csproj 文件中向该组件添加一个<remove>项。 Just remove it.只需将其删除。

Using asp.net core 3.1, and blazor 3.2 preview 2使用 asp.net 内核 3.1 和 blazor 3.2 预览 2

@maksymiuk is absolutely correct on this one. @maksymiuk 在这方面是绝对正确的。 Change the Router tag in App.Razor file in Core 3.1 and the external routings are included.在 Core 3.1 中更改 App.Razor 文件中的 Router 标签,并包含外部路由。

<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.有趣的是,只要您从 Razor 库中包含 1 个组件,您就会发现其他组件中的所有其他路由(至少在相同的区域/页面文件夹中)也可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM