Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

routes - ASP.NET Core 3.1 areas return 404

I tried this link Stack Link FOR 404 Error but error not going showing 404 error for area

 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();
        }
    }
}

Two Areas Student and Admin i am using please refer link below

Structure Image

enter image description here

controller

 namespace BCA_New_System.Areas.Student.Controllers
{
  public class StudentExamController : Controller
  {
    public IActionResult Index()
    {
        return View();
    }
  }
}

index page

@{
ViewData["Title"] = "Index";
Layout = "~/Areas/Student/Views/Shared/_Layout.cshtml";
 }

 <h1>Index</h1>
question from:https://stackoverflow.com/questions/65913949/asp-net-core-3-1-areas-return-404

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you can add a pattern like this for different areas. For student area you add this code snippet:

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

In your controller you have to add annotation of area like this:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...