'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";
}
Solution 1:[1]
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
Solution 2:[2]
Add area in your controller , it will do the trick
Solution 3:[3]
I found that as I am using a number of areas I have been a bit more implicit. Not only do I declare the Area Attribte but also state the area.
[Area("Landing")]
This worked for me.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Saurav |
Solution 2 | Sougata Mudi |
Solution 3 | Shane Atkinson |