无法找到页面 - razor 页面项目中的 .razor,Net 5.0

Page can’t be found - .razor in razor pages project, Net 5.0

我在 Net 5.0 上有一个 razor pages 项目。在这个项目中,我想同时使用 .cshtml 和 .razor 页面。 (也许随着时间迁移到 .razor)。我可以在 .cshtml 页面上成功使用 razor 组件,但我无法使 .razor 页面正常工作,总是出现“无法找到此页面" 错误。 我关注了 在单个 ASP.NET Core 3 应用程序 和其他一些应用程序中组合 Razor 和 Blazor 页面,但仍然不起作用。

Startup.cs

services.AddServerSideBlazor();
and
endpoints.MapBlazorHub();
endpoints.MapFallbackToFile("/_Host.cshtml")

_Imports.razor 在根目录中:

@using System.Net.Http
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop

Pages/App.razor

@using Microsoft.AspNetCore.Components.Routing 
 <Router AppAssembly="@typeof(Program).Assembly"}">
    <Found Context="routeData">
        <RouteView RouteData="@routeData"/>
    </Found>
    <NotFound>
        <h1>Page not found123</h1>
        <p>Sorry, but there's nothing here!</p>
    </NotFound>
</Router>

Page/_Host.cshtml

@page "/blazor"
 
@{
    Layout = "_Layout";
}
 
<div id="app">
    @(await Html.RenderComponentAsync<App>(RenderMode.Server))
</div>

Page/Shared/_Layout.cshtml

<script src="_framework/blazor.server.js"></script>

 @RenderSection("Scripts", required: false)

页面本身:Pages/Test.razor

@page "/test"
@implements IDisposable
<div>test</div>

我错过了什么?

这是我的剃刀页面项目结构:

我将共享文件夹添加到项目中,并将App.razor、_Imports.razor添加到project.Add Counter.razor、_Host.cshtml到页面Folder.Add配置在Startup.cs.

Startup.cs:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddServerSideBlazor();
            services.AddRazorPages();
        }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
                endpoints.MapRazorPages();
            });
        }

_Imports.razor(最后两行RazorPageDemo5._0是我的项目名,你的可以是@using projectname @using projectname.Shared):

@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.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using Microsoft.JSInterop
@using RazorPageDemo5._0
@using RazorPageDemo5._0.Shared

Page/Shared/_Layout.cshtml:

<script src="_framework/blazor.server.js"></script>
    @await RenderSectionAsync("Scripts", required: false)

Page/_Host.cshtml(将命名空间更改为您项目的命名空间):

@page "/"
@namespace RazorPageDemo5._0.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="~/" />
    <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
    <link href="css/site.css" rel="stylesheet" />
</head>
<body>
    <component type="typeof(App)" render-mode="ServerPrerendered" />

    <div id="blazor-error-ui">
        <environment include="Staging,Production">
            An error has occurred. This application may no longer respond until reloaded.
        </environment>
        <environment include="Development">
            An unhandled exception has occurred. See browser dev tools for details.
        </environment>
        <a href="" class="reload">Reload</a>
        <a class="dismiss"></a>
    </div>

    <script src="_framework/blazor.server.js"></script>
</body>
</html>

然后将MainLayout.razor,NavMenu.Razor,SurveyPrompt.razor添加到Shared folder.Add App.razor到project.

结果: