Routes ASP.NET核心3.1区域返回404
我尝试了这个链接,但没有显示404区域错误Routes ASP.NET核心3.1区域返回404,routes,asp.net-core-mvc,asp.net-core-3.1,area,Routes,Asp.net Core Mvc,Asp.net Core 3.1,Area,我尝试了这个链接,但没有显示404区域错误 public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use th
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();
}
}
}
索引页
@{
ViewData["Title"] = "Index";
Layout = "~/Areas/Student/Views/Shared/_Layout.cshtml";
}
<h1>Index</h1>
@{
ViewData[“标题”]=“索引”;
Layout=“~/Areas/Student/Views/Shared/_Layout.cshtml”;
}
指数
您可以为不同的区域添加这样的图案。对于学生区,添加以下代码段:
endpoints.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}"
);
在控制器中,必须添加如下区域注释:
public class StudentExamController : Controller
{
[Area("Student")]
public IActionResult Index()
{
return View();
}
}
你能试试这个图案吗?MapControllerOute(名称:“区域”,模式:“{area:exists}/{controller=Dashboard}/{action=Index}/{id?}”);我应该删除或添加哪一行?删除您的学生地图区域控制器端点并使用上述命令抱歉,第一条注释有错误。您可以删除您的学生地图区域控制器行并添加此命令吗?MapControllerOute(名称:“区域”,模式:“{area:exists}/{controller=StudentExam}/{action=Index}/{id?}”);不工作,我添加了你的问题代码见上文,我尝试了这个url,确定与否,这是dotnetcore的
public class StudentExamController : Controller
{
[Area("Student")]
public IActionResult Index()
{
return View();
}
}