【发布时间】:2021-01-27 06:30:21
【问题描述】:
我尝试了这个链接Stack Link FOR 404 Error,但错误不会显示区域的 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();
}
}
}
我正在使用的两个领域学生和管理员请参考下面的链接
结构图
控制器
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=Dashboard}/{action=Index}/{id?}");
-
我应该删除或添加哪一行?
-
删除您的学生地图区域控制器端点并使用上述
-
对不起,第一条评论有错误,你能删除你的学生 mapareacontrollerroute 行并添加这个吗? endpoints.MapControllerRoute(name: "areas", pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}");
-
不工作我添加了你有问题的代码见上面我试过这个网址localhost:59958/Student/StudentExam/Index它是否正常
标签: routes asp.net-core-mvc asp.net-core-3.1 area