Shane Atkinson
Shane Atkinson

Reputation: 31

.net core 6.0 and scaffolding identity

I have an issue around using Areas and Scaffolding Identity in .net core 6. I have tried numerous ways to resolve the issue and the following are the attempts. I'll post code at the end.

  1. Created a new MVC project and included Identity from the start. Without using Areas the site works correctly. Once I implement areas by scaffolding areas into the site, and moving all of the relevant files when I run the site it returns a blank page. I can manually navigate to the identity pages and they still work.
  2. Created a new MVC project with no Identity, scaffolded Areas and the site works correctly. Then scaffolded Identity into the project and the home page stopped functioning. Can still navigate to the Identity pages which work correctly.

program.cs

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using ObferoTest.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    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.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
      name: "areas",
      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
    );
});
app.MapRazorPages();

app.Run();

HomeController.cs

using Microsoft.AspNetCore.Mvc;

namespace ObferoTest.Areas.Landing.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

If there is anything else I need to post please let me know and I will do so.

Upvotes: 1

Views: 1751

Answers (1)

Shane Atkinson
Shane Atkinson

Reputation: 31

I have found the issue after many hours of working through this. With .net core 6 you are not required to explicitly state the area attribute. This can be shown when you scaffold an Area which I did and it works. Once you Scaffold Identity you must add the Area Attribute at the beginning of your controller.

[Area("Landing")]
public class HomeController : Controller
{

Once I added the attribute the site functions correctly.

Upvotes: 1

Related Questions