Razor组件标记辅助程序未实际加载Razor组件

Razor组件标记辅助程序未实际加载Razor组件,razor,blazor,Razor,Blazor,我一直在按照中的步骤在我的Razor应用程序中设置Blazor组件。我完成了该指南“准备应用程序”部分的所有步骤,修改了_Layout.cshtml和Startup.cs文件,并添加了_Imports.razor文件。为了测试这一点,我只是尝试实现一个基本的计数器组件 我在MyApp/Components/Counter.razor中添加了以下代码: <p>Current count: @currentCount</p> <button class="bt

我一直在按照中的步骤在我的Razor应用程序中设置Blazor组件。我完成了该指南“准备应用程序”部分的所有步骤,修改了_Layout.cshtml和Startup.cs文件,并添加了_Imports.razor文件。为了测试这一点,我只是尝试实现一个基本的计数器组件

我在MyApp/Components/Counter.razor中添加了以下代码:

<p>Current count: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {
    private int currentCount = 0;

    [Parameter]
    public int InitialValue { get; set; }

    private void IncrementCount() => currentCount++;

    protected override void OnParametersSet()
    {
        currentCount = InitialValue;
    }
}
当前计数:@currentCount

点击我 @代码{ 私有int currentCount=0; [参数] 公共int初始值{get;set;} 私有void IncrementCount()=>currentCount++; 受保护的覆盖无效OnParametersSet() { currentCount=初始值; } } 然后在MyApp/Pages/Counter.cshtml中,我有以下内容:

@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Web
@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.JSInterop
@using MyApp
@using MyApp.Components

//This does not work--it appears exactly like this in the HTML when the page loads
<component type="typeof(Counter)" render-mode="ServerPrerendered" />

//this works as expected and loads the razor component
@(await Html.RenderComponentAsync<Counter>(RenderMode.ServerPrerendered))
@page
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@使用Microsoft.AspNetCore.Components
@使用Microsoft.AspNetCore.Components.Web
@使用System.Net.Http
@使用Microsoft.AspNetCore.Authorization
@使用Microsoft.AspNetCore.Components.Authorization
@使用Microsoft.AspNetCore.Components.Forms
@使用Microsoft.AspNetCore.Components.Routing
@使用Microsoft.JSInterop
@使用MyApp
@使用MyApp.Components
//这不起作用——当页面加载时,它在HTML中的显示与此完全相同
//这将按预期工作并加载razor组件
@(等待Html.RenderComponentAsync(RenderMode.ServerPrerendered))

注意,我从_Imports.razor文件中复制了所有的using指令,以查看这是否解决了问题,但这并没有什么区别。我的理解是RenderComponentAsync函数已经过时,“component”标记助手是当前使用razor组件的方式。我也更喜欢使用这种语法,因为传递参数更容易。有人知道我缺少了什么才能让它正常工作吗?

好吧,在花了几个小时的时间胡思乱想之后,我意识到我的应用程序是在Net Core 3.0上的,而tag helper仅在3.1+中可用。更新MyApp.csproj,改为使用3.1修复它:

<TargetFramework>netcoreapp3.1</TargetFramework> 
netcoreapp3.1