Search code examples
c#.netasp.net-coreaspnetboilerplate

.NET Core 3 - my Area tag is not being recognised


Hopefully there's something incredibly obvious I'm missing, but I've been at this a day so far with absolutely no luck - I've searched through 10+ Area tutorials to see if I can spot anything, but to no avail.

Here's my folder layout:

enter image description here

And endpoint routing:

enter image description here

(also tried):

enter image description here

and Controller:

enter image description here

and View:

enter image description here

Yet, with whatever I've tried, I'm always getting this in the HTML generation...

enter image description here

Note the ?area=Admin in the form.

Surely I'm doing everything right?! I've done this a million times in .NET MVC + .NET Core 2.x


Solution

  • I did not manage to reproduce the error. However, I have managed to create the example which works. Steps to create the MWE


    tl;dr (if won't work read all :D ):

    Look at the 4th point. It seems that the specified name of the root folder of your areas is incorrect. See the docs


    1) dotnet new mvc

    2) Install the latest Aspnetboilerplate dotnet add package Abp.AspNetCore --version 5.0.0

    3) Create the Areas\Admin\ directories with Views and Controllers (like on your screenshots)

    4) Set up the routing (this may be the crucial part) in the Startup.cs:

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

    5) Areas\Admin\Controllers\BankHolidaysController.cs

    using System;
    using Abp.AspNetCore.Mvc.Controllers;
    using Microsoft.AspNetCore.Mvc;
    
    namespace dotnet_mvc_areas_stack.Areas.Admin.Controllers
    {
        [Area("Admin")]
    
        public class BankHolidaysController : AbpController
        {
    
            public IActionResult Index()
            {
                return View();
            }
    
            public IActionResult Create()
            {
                Console.WriteLine("Creating");
                return View();
            }
        }
    }
    

    6) Areas\Views\BankHolidays\Create.cshtml with the (degenerated :) ) form

    <p> This is index page of area </p>
    <div>
        <form method="post" action="/Admin/BankHolidays/Create">
            <button type="submit">submit</button>
        </form>
    </div>
    

    7) Create.cshtml next to it

    <div>Created</div>