【问题标题】:ASP.NET Core 3.1 areas return 404ASP.NET Core 3.1 区域返回 404
【发布时间】: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


【解决方案1】:

您可以为不同的区域添加这样的模式。对于学生区,您添加此代码 sn-p:

endpoints.MapControllerRoute(
                    name: "areas",
                    pattern: "{area:exists}/{controller=StudentExam}/{action=Index}/{id?}"
);

在您的控制器中,您必须像这样添加区域注释:

public class StudentExamController : Controller
  {
    [Area("Student")]
    public IActionResult Index()
    {
        return View();
    }
  }

【讨论】:

  • 它是用于dotnetcore的
猜你喜欢
  • 1970-01-01
  • 2020-07-10
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 2020-09-01
  • 2020-08-02
相关资源
最近更新 更多