C
C#12mo ago
EnderWolf

mvc not finding blazor item, 404 not found

Hello! so, I've got a question if you don't mind answering. I made an MVC in .net 6 which I then later updated to .net 8. a few days ago I added a new blazor page to it, which works completely fine. they can be rendered inside views/other pages completely fine. however, I'm now trying to get it so that another blazor page opens when a button is clicked. this seems to just give me a 404 error the page that's open is:Progect\Components\Dash.razorthe page I am trying to open is:Progect\Components\Counter.razor
@inject NavigationManager NavigationManager
@using Project.Components

<div class="container-fluid">
<div class="row flex-nowrap">
class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark">
<div class="d-flex flex-column align-items-center align-items-sm-start px-3 pt-2 text-white min-vh-100">
<a href="/" class="d-flex align-items-center pb-3 mb-md-0 me-md-auto text-white text-decoration-none">
<span class="fs-5 d-none d-sm-inline">Menu</span>
</a>

<ul class="nav nav-pills flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start" id="menu">
<li class="nav-item">
<a href="#" class="nav-link align-middle px-0">
<i class="fs-4 bi-house"></i> <span class="ms-1 d-none d-sm-inline">Home</span>
</a>
</li>
<li>
<a href="#" class=" px-0 align-middle">
<i class="fs-4 bi-table"></i> <span @onclick="NavigateToCounter" class="ms-1 d-none d-sm-inline">View Submissions</span>
</a>
</li>
</ul>
</div>
</div>
<div class="col py-3">
<a class="nav-link" asp-controller="Submission" asp-action="Index">View Submissions</a>
Content area...
</div>
</div>
</div>
@code {
private void NavigateToCounter()
{
NavigationManager.NavigateTo("/counter", forceLoad: true);
}
}
@inject NavigationManager NavigationManager
@using Project.Components

<div class="container-fluid">
<div class="row flex-nowrap">
class="col-auto col-md-3 col-xl-2 px-sm-2 px-0 bg-dark">
<div class="d-flex flex-column align-items-center align-items-sm-start px-3 pt-2 text-white min-vh-100">
<a href="/" class="d-flex align-items-center pb-3 mb-md-0 me-md-auto text-white text-decoration-none">
<span class="fs-5 d-none d-sm-inline">Menu</span>
</a>

<ul class="nav nav-pills flex-column mb-sm-auto mb-0 align-items-center align-items-sm-start" id="menu">
<li class="nav-item">
<a href="#" class="nav-link align-middle px-0">
<i class="fs-4 bi-house"></i> <span class="ms-1 d-none d-sm-inline">Home</span>
</a>
</li>
<li>
<a href="#" class=" px-0 align-middle">
<i class="fs-4 bi-table"></i> <span @onclick="NavigateToCounter" class="ms-1 d-none d-sm-inline">View Submissions</span>
</a>
</li>
</ul>
</div>
</div>
<div class="col py-3">
<a class="nav-link" asp-controller="Submission" asp-action="Index">View Submissions</a>
Content area...
</div>
</div>
</div>
@code {
private void NavigateToCounter()
{
NavigationManager.NavigateTo("/counter", forceLoad: true);
}
}
The page to open
@page "/counter"
<h1>Counter</h1>
<p role="status">CurrentCount: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click to increment</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
@page "/counter"
<h1>Counter</h1>
<p role="status">CurrentCount: @currentCount</p>
<button class="btn btn-primary" @onclick="IncrementCount">Click to increment</button>
@code {
private int currentCount = 0;
private void IncrementCount()
{
currentCount++;
}
}
2 Replies
ineternet
ineternet12mo ago
can you show your program.cs
EnderWolf
EnderWolfOP12mo ago
yup, sure. I did have to remove comments due to message length.
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Project.Data;
using Project.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddServerSideBlazor();
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<WeatherForecastService>();

var connString = builder.Configuration.GetConnectionString("Default");
builder.Services.AddDbContext<ProjectContext>(options =>
{
options.UseSqlServer(connString);
});

builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Strict;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/Login/Login";
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.SlidingExpiration = true;
options.AccessDeniedPath = "/Login/AccessDenied";
});

builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Management", policy => policy.RequireRole("Admin", "Manager").RequireClaim("Department", "Management"));
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

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

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseSession();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapBlazorHub();
app.Run();
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;
using Project.Data;
using Project.Models;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddServerSideBlazor();
builder.Services.AddControllersWithViews();
builder.Services.AddSingleton<WeatherForecastService>();

var connString = builder.Configuration.GetConnectionString("Default");
builder.Services.AddDbContext<ProjectContext>(options =>
{
options.UseSqlServer(connString);
});

builder.Services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromMinutes(10);
options.Cookie.HttpOnly = true;
options.Cookie.SameSite = SameSiteMode.Strict;
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options =>
{
options.LoginPath = "/Login/Login";
options.ExpireTimeSpan = TimeSpan.FromMinutes(10);
options.SlidingExpiration = true;
options.AccessDeniedPath = "/Login/AccessDenied";
});

builder.Services.AddAuthorization(options =>
{
options.AddPolicy("Management", policy => policy.RequireRole("Admin", "Manager").RequireClaim("Department", "Management"));
});

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

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

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.UseSession();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapBlazorHub();
app.Run();
Want results from more Discord servers?
Add your server