Saurav
Saurav

Reputation: 640

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:

enter image description here

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";
}

Upvotes: 0

Views: 1351

Answers (3)

Shane Atkinson
Shane Atkinson

Reputation: 31

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.

Upvotes: 1

Sougata Mudi
Sougata Mudi

Reputation: 51

Add area in your controller , it will do the trick

Upvotes: 1

Saurav
Saurav

Reputation: 640

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

Upvotes: 0

Related Questions