在使用.net核心6中的区域和脚手架标识时,我遇到了一个问题。我尝试过很多方法来解决这个问题,下面是一些尝试。我会在最后贴上密码。
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();
}
}
}如果我还需要发什么东西,请告诉我,我会这样做的。
发布于 2022-04-26 08:40:21
经过几个小时的努力,我发现了这个问题。对于.net核心6,您不需要显式地声明area属性。这可以显示当你脚手架的领域,我做了,它的工作。一旦您获得身份,就必须在控制器的开头添加Area属性。
[Area("Landing")]
public class HomeController : Controller
{一旦我添加了属性,站点功能就正确了。
https://stackoverflow.com/questions/71996853
复制相似问题