I'm writing an ASP.Net Core app based on Freeman's book. And while adding Blazor pages, I got a problem. When you try to go to pages that Blazor is connected to, nothing is displayed on them.
The browser console shows these errors:
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)
My Routed.razor file:
<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>
What could be the problem?
I also add a link to the repository of my project:
I had to "Hack" your Program
to get your solution running. Here's my version:
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();
}
}
}
I then update Routed.razor - no duplication of assemblies any more which was the main issue.
<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>
And a quick fix to Index to get the references correct:
@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>
And I get: