Blazor Core托管-Razor类库项目中的所有组件都以HTML格式打印在页面上

时间:2019-10-11 18:45:38

标签: .net-core blazor razor-class-library

我找不到类似问题的关键字,所以我要发布一个新的关键字。

该项目运行正常,突然发生了以下问题。我只编辑了Razor类库中的一些Razor文件,因此无法将其追溯到我所做的任何代码修改。

问题:

我具有以下项目结构:

enter image description here

客户端,服务器和共享是最初由NET Core 3.0中的Blazor模板创建的项目。 DesktopClient 是与ElectronNET类似的Blazor项目,那里的组件上也出现了问题。 SharedLibrary 项目是所有项目共同的组件所在的项目,问题的根源显然来自哪里。

客户端项目中引用了 SharedLibrary ,因此我可以访问客户端项目中的组件。

发生的事情是,突然,Razor类库中的组件停止“编译”,所有“ @”都以Raw HTML格式打印,就像“ Counter 2”的按钮一样:

[enter image description here]

注意图像上有3个按钮。第一个在基础Razor页面中,并且正在正确编译。第二个来自 SharedLibrary 项目,由于<@>标签没有被替换 d,显然未编译。第三个按钮来自 Client 项目(与基础Razor页面相同的项目)中的组件,并且也可以正常工作。

所以问题是 SharedLibrary Razor文件没有被“编译”。

我尝试过的事情:

  • 从项目中删除并添加引用;
  • 清洁并重建解决方案;
  • 更新软件包;
  • 检查了.csproj,以查看是否发现任何错误/缺失(此文件没有经验,因此可能遗漏了一些东西)。
  • 创建了一个新的Blazor Core Hosted项目,并添加了具有与该演示相同的设置的Razor类库。它工作正常,所以这个项目存在问题。依赖关系和“ .csproj”文件看起来非常相似。

代码:

.csproj用于客户端项目:     

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <OutputType>Exe</OutputType>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Blazor.Extensions.SignalR" Version="0.4.0" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview9.19465.2" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview9.19465.2" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.0.0-preview9.19465.2" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.0.0-preview9.19465.2" PrivateAssets="all" />
    <PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
  </ItemGroup>
  <ItemGroup>
    <ProjectReference Include="..\..\SharedLibrary\SharedLibrary.csproj" />
    <ProjectReference Include="..\Shared\ServerAPI.Shared.csproj" />
  </ItemGroup>

  <ItemGroup>
    <Content Include="..\..\SharedLibrary\Content\styles\font\**\*.*">
      <Link>wwwroot\styles\font\%(RecursiveDir)%(Filename)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="..\..\SharedLibrary\Content\images\system\*.*">
      <Link>wwwroot\images\system\%(RecursiveDir)%(Filename)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Content Include="..\..\SharedLibrary\Content\images\social\brands\*.*">
      <Link>wwwroot\images\social\brands\%(RecursiveDir)%(Filename)%(Extension)</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    <Compile Remove="bin\**" />
    <Content Remove="bin\**" />
    <EmbeddedResource Remove="bin\**" />
    <None Remove="bin\**" />
    <Content Remove="compilerconfig.json" />
  </ItemGroup>

  <ItemGroup>
    <_ContentIncludedByDefault Remove="compilerconfig.json" />
    <_ContentIncludedByDefault Remove="wwwroot\styles\main.css" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Properties\" />
    <Folder Include="Services\" />
    <Folder Include="wwwroot\images\system\" />
    <Folder Include="wwwroot\scripts\" />
  </ItemGroup>
  <ItemGroup>
    <None Include="compilerconfig.json" />
  </ItemGroup>
  <ItemGroup>
    <Content Update="wwwroot\styles\main.css">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

  <Target Name="CopyLinkedContentFiles" BeforeTargets="Build">
    <Copy SourceFiles="%(Content.Identity)" DestinationFiles="%(Content.Link)" SkipUnchangedFiles="true" OverwriteReadOnlyFiles="true" Condition="'%(Content.Link)' != ''" />
  </Target>

</Project>

.csproj for SharedLibrary

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <OutputType>Library</OutputType>
    <IsPackable>true</IsPackable>
    <BlazorLinkOnBuild>false</BlazorLinkOnBuild>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
    <RazorLangVersion>3.0</RazorLangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Blazor.Extensions.SignalR" Version="0.4.0" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview9.19465.2" />
    <PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview9.19465.2" PrivateAssets="all" />
    <PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="3.0.0" />
  </ItemGroup>

  <ItemGroup>
    <Folder Include="Content\images\social\brands\" />
    <Folder Include="Core\Login\" />
    <Folder Include="Core\Login\OAuth2\" />
    <Folder Include="Services\" />
  </ItemGroup>

  <ItemGroup>
    <Compile Update="Resources\Languages\en-US.Designer.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>en-US.resx</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <EmbeddedResource Update="Resources\Languages\en-US.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>en-US.Designer.cs</LastGenOutput>
    </EmbeddedResource>
  </ItemGroup>

</Project>

Index.razor

@page "/"

@using ServerAPI.Client.Components
@using SharedLibrary.Components.User

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    int currentCount = 0;

    void IncrementCount()
    {
        currentCount++;
    }
}
<SocialWithFormLoginComponent />
<Counter />

SharedLibrary项目中的SocialWithFormLoginComponent.razor

<h1>Counter 2</h1>

<p>Current count 2: @currentCount2</p>

<button class="btn btn-primary" @onclick="IncrementCount2">Click me 2</button>

@code {
    int currentCount2 = 0;

    void IncrementCount2()
    {
        currentCount2++;
        StateHasChanged();
        Console.WriteLine("Hello world!");
    }
}

客户项目中的Counter.razor

<h1>Counter 3</h1>

<p>Current count 3: @currentCount3</p>

<button class="btn btn-primary" @onclick="IncrementCount3">Click me 3</button>

@code {
    int currentCount3 = 0;

    void IncrementCount3()
    {
        currentCount3++;
        StateHasChanged();
        Console.WriteLine("Hello world!");
    }
}

最终评论

如您所见,该代码对于我的故障排除非常简单,但是该项目并非如此,因此重新创建将需要大量工作。

有什么建议可以尝试吗?我真的很困惑。

谢谢!

更新解决方案:

结果证明,如@dani hererra所述,我在附加的Razor类库中的组件文件夹中缺少“ _Imports.razor”,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:1)

检查sub search { my $term = shift; my %opts = @_; ... if ($opts{case_insensitive}) { ... } ... } search("foo"); search("foo", case_insensitive => 0); search("foo", case_insensitive => 1); 列在Microsoft.AspNetCore.Components.Web上:

_Imports.razor