locked
Route in Asp.Net Core 3.1 RRS feed

  • Question

  • User338455301 posted

    Hi,
    I create new project asp.core 3.1 mvc and add admin area 
    i have home controller in admin arae but when i call action  /admin/home its not redirect to index  action and its show not found(404)
    i must write full path /admin/home/index to call index action 
    i have this problem just in core 3  no have any problem in core 2

    startup

    public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(
                        Configuration.GetConnectionString("DefaultConnection")));
                services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<ApplicationDbContext>();
                services.AddControllersWithViews();
                services.AddRazorPages();
            }
    
            // 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();
                    app.UseDatabaseErrorPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
                app.UseHttpsRedirection();
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthentication();
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
    
                    endpoints.MapRazorPages();
                    endpoints.MapAreaControllerRoute(
      name: "areas", "areas",
      pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
                });

    controller in area 

    [Area("Admin")]
    
        public class HomeController : Controller
        {
            // GET: Home
            public ActionResult Index()
            {
                return View();
            }
    }

    my proejct 
    http://s7.picofile.com/file/8386230200/WebApplication4.rar.html

    Monday, January 27, 2020 9:40 AM

All replies

  • User665608656 posted

    Hi elahi1mahdi,

    According to your description, in your Configure method, you need to change the second "areas" to the area folder name you created, which is "admin".

    And I suggest you write the endpoints.MapAreaControllerRoute method before the endpoints.MapControllerRoute method to avoid other errors.

    app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
    endpoints.MapAreaControllerRoute( name: "areas", "admin", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); });

    You can also refer to this link:

    How to define an endpoint Route to multiple Areas

    Best Regards,

    YongQing.

    Tuesday, February 4, 2020 7:47 AM