为什么 Blazor 页面没有在我的 ASP.Net 应用程序中触发

Why are Blazor pages not firing in my ASP.Net app

我正在根据弗里曼的书编写 ASP.Net 核心应用程序。 在添加 Blazor 页面时,我遇到了问题。当您尝试转到 Blazor 连接到的页面时,页面上没有显示任何内容。

浏览器控制台显示这些错误:

Error: System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.AspNetCore.Components.Routing.Router.Refresh(Boolean isNavigationIntercepted) at Microsoft.AspNetCore.Components.Routing.Router.SetParametersAsync(ParameterView parameters)

我的 Routed.razor 文件:

<Router AppAssembly="@typeof(Program).Assembly"
    AdditionalAssemblies="new[]{typeof(Program).Assembly}">
<Found>
    <RouteView RouteData="@context" DefaultLayout="typeof(AdminLayout)" />
</Found>
<NotFound>
    <h4 class="bg-danger text-white text-center p-2">
        No Matching Route Found
    </h4>
</NotFound>

可能是什么问题?

我还添加了一个 link 到我的项目的存储库中:

https://github.com/itehnoviking/CoinStore

我不得不“破解”您的 Program 以获得您的解决方案 运行。这是我的版本:

using CoinStore.Models;
using Microsoft.EntityFrameworkCore;

namespace CoinStore
{
    public class Program
    {

        public static void Main(string[] args)
        {


            var builder = WebApplication.CreateBuilder(args);

            var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

            builder.Services.AddDbContext<StoreDbContext>(opt => opt.UseSqlServer(connectionString));

            builder.Services.AddScoped<IStoreRepository, EFStoreRepository>();
            builder.Services.AddScoped<IOrderRepository, EFOrderRepository>();
            builder.Services.AddRazorPages();
            builder.Services.AddDistributedMemoryCache();
            builder.Services.AddSession();
            builder.Services.AddScoped<Cart>(sp => SessionCart.GetCart(sp));
            builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //builder.Services.AddServerSideBlazor();

            //builder.Services.AddServerSideBlazor().AddCircuitOptions(o => {
            //    o.DetailedErrors = _env.IsDevelopment;
            //}).AddHubOptions(opt => {
            //    opt.MaximumReceiveMessageSize = 10 * 1024 * 1024; // 10MB
            //});

            builder.Services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });





            // Add services to the container.
            builder.Services.AddControllersWithViews();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Home/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.UseDeveloperExceptionPage();
            //app.UseStatusCodePages();
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseSession();
            app.UseRouting();

            app.UseAuthorization();

            //app.UseEndpoints(endpoints => {
            //    endpoints.MapControllerRoute("catpage",
            //        "{category}/Page{productPage:int}",
            //        new { Controller = "Home", action = "Index" });

            //    endpoints.MapControllerRoute("page", "Page{productPage:int}",
            //        new { Controller = "Home", action = "Index", productPage = 1 });

            //    endpoints.MapControllerRoute("category", "{category}",
            //        new { Controller = "Home", action = "Index", productPage = 1 });

            //    endpoints.MapControllerRoute("pagination",
            //        "Products/Page{productPage}",
            //        new { Controller = "Home", action = "Index", productPage = 1 });
            //    endpoints.MapDefaultControllerRoute();
            //    endpoints.MapRazorPages();
            //    endpoints.MapBlazorHub();
            //    endpoints.MapFallbackToPage("/admin/{*catchall}", "/Admin/Index");
            //});


            //app.MapControllerRoute("catpage", "{category}/Page{productPage:int}", new { controller = "Home", action = "index" });
            //app.MapControllerRoute("page", "Page{productPage:int}", new { controller = "Home", action = "index", productPage = 1 });
            //app.MapControllerRoute("category", "{category}", new { controller = "Home", action = "index", productPage = 1 });
            //app.MapControllerRoute("pagination", "Products/Page{productPage}", new { controller = "Home", action = "index", productPage = 1 });

            //app.MapControllerRoute(
            //    name: "default",
            //    pattern: "{controller=Home}/{action=Index}/{id?}");

            //app.MapDefaultControllerRoute();
            app.MapRazorPages();
            app.MapBlazorHub();
            app.MapFallbackToPage("/admin/{*catchall}", "/Admin/Index");
            


            //SeedData.EnsurePopulated(app);

            app.Run();

        }
    }
}

然后我更新 Routed.razor - 程序集不再重复,这是主要问题。

<Router AppAssembly="@typeof(Program).Assembly">
    <Found>
        <RouteView RouteData="@context" DefaultLayout="typeof(AdminLayout)" />
    </Found>
    <NotFound>
        <h4 class="bg-danger text-white text-center p-2">
            No Matching Route Found
        </h4>
    </NotFound>
</Router>

并快速修复索引以使引用正确:

@page "/admin"
@{
    Layout = null;
}

<!DOCTYPE html>
<html>
    <head>
        <title>CoinStore Admin</title>
        <link href="/lib/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" />
        <base href="~/" />
    </head>
    <body>
        <component type="typeof(Routed)" render-mode="Server" />
        <script src="/_framework/blazor.server.js"></script>
    </body>
</html>

我得到: