Reputation: 31
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.
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
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