ASP.NET 核心 3.1 领域 return 404
ASP.NET Core 3.1 areas return 404
我试过这个 link 但是错误不显示区域
的 404 错误
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<BCAContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BCAContext")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
//endpoints.MapAreaControllerRoute(
// "Student",
// "Student",
// "Student/{controller=StudentExam}/{action=Index}/{id?}");
endpoints.MapControllerRoute(name: "areas", pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
//database
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<BCAContext>();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
//context.Database.Migrate();
}
}
}
两个区域Student和Admin我正在使用请参考下面的link
结构图
控制器
namespace BCA_New_System.Areas.Student.Controllers
{
public class StudentExamController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
索引页
@{
ViewData["Title"] = "Index";
Layout = "~/Areas/Student/Views/Shared/_Layout.cshtml";
}
<h1>Index</h1>
您可以为不同的区域添加这样的图案。对于学生区,您添加此代码段:
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}"
);
在您的控制器中,您必须像这样添加区域注释:
public class StudentExamController : Controller
{
[Area("Student")]
public IActionResult Index()
{
return View();
}
}
我试过这个 link
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddDbContext<BCAContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("BCAContext")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
//endpoints.MapAreaControllerRoute(
// "Student",
// "Student",
// "Student/{controller=StudentExam}/{action=Index}/{id?}");
endpoints.MapControllerRoute(name: "areas", pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
//database
using (var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope())
{
var context = serviceScope.ServiceProvider.GetRequiredService<BCAContext>();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
//context.Database.Migrate();
}
}
}
两个区域Student和Admin我正在使用请参考下面的link
结构图
控制器
namespace BCA_New_System.Areas.Student.Controllers
{
public class StudentExamController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
索引页
@{
ViewData["Title"] = "Index";
Layout = "~/Areas/Student/Views/Shared/_Layout.cshtml";
}
<h1>Index</h1>
您可以为不同的区域添加这样的图案。对于学生区,您添加此代码段:
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}"
);
在您的控制器中,您必须像这样添加区域注释:
public class StudentExamController : Controller
{
[Area("Student")]
public IActionResult Index()
{
return View();
}
}