简体   繁体   中英

404 error after scaffolding identity library of an ASP.NET Core 6 MVC project

I am using .NET 6. Every time I scaffold Identity, the application stops working.

My Startup class is like this:

using BulkBook.Data.Data;
using BulkBook.Data.Repository;
using BulkBook.Data.Repository.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Identity;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<BulkDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("BuldDbConnection")));
//builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
//    .AddEntityFrameworkStores<BulkDbContext>();
builder.Services.AddDefaultIdentity<IdentityUser>()
    .AddEntityFrameworkStores<BulkDbContext>();

builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddRazorPages().AddRazorRuntimeCompilation();

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.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapRazorPages();

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

app.Run();

Here is the folder structure:

在此处输入图像描述

If I remove the Identity folder, then things start to work again.

_ViewStart.cshtml of identity contains the following code

@{
    Layout = "/Views/Shared/_Layout.cshtml";
}

I had used Area configuration to arrange the controllers. However, After adding Idenity library I don't why routing does not work. In order for this to work, I just decorated the controller classes with

[Area]

attribute and passed the name of the area. This solved to issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM