routes - ASP.NET Core 3.1 区域返回 404

标签 routes asp.net-core-mvc asp.net-core-3.1 area

我试过这个链接 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();
        }
    }
}
两区学生 管理员 我正在使用请引用下面的链接
结构图
enter image description here
Controller
 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?}"
);
在您的 Controller 中,您必须像这样添加区域注释:
public class StudentExamController : Controller
  {
    [Area("Student")]
    public IActionResult Index()
    {
        return View();
    }
  }

关于routes - ASP.NET Core 3.1 区域返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65913949/

相关文章:

Extjs 路由器传递多个参数

angular - 在 canActivate 中获取父路由参数

javascript - React-router v4 - 无法获取 *url*

asp.net - 用于多种语言的 ASP.NET 路由的正则表达式

c# - 没有外部登录提供程序的 ASP.NET Core MVC 2.2 登录

.net - 为什么 ASP.NET Core Web 应用程序中有 Main() 方法(入口方法)?这种方法背后的原因是什么?

c# - 登录成功后授权页面重定向回登录

c# - ASP.NET Core 项目中的 MySql 连接字符串错误

Azure Web 应用程序 (Windows) - 在调用 Entity Framework Core 的 CILJit::compileMethod 期间停止

asp.net-core - 包 Microsoft.AspNetCore.Authentication.JwtBearer 5.0.0 与 netcoreapp3.1 不兼容,但它的目标是 net 5.0