0

I have 2 questions, but they both relate to one key concept: what do I need to add to Routing to handle pages inside an Area ?

  1. I created an Area Admin in my ASP.NET Core 3.0 MVC application. I would like to be able to visit the pages like this:

    a) /Admin/    ...to show the Index page
    b) /Admin/aa  ... show the view "aa"
    c) /Admin/bb  ... show the view "bb"
    

    The browser just returns a blank page. No errors or exceptions shown in the logs. I suspect the routing is incorrect.

  2. I did add a new scaffolded item inside the Admin area (described above in question 1), and chose "MVC Controller with views, using Entity Framework" for Company. After selecting the model, and other details, the VS2019 scaffolding generated the controller and the corresponding views (Create, Delete, Details, Edit, Index). But what routing code do I need to add to accommodate the newly generated URLs for Company?

Here is the routing code in the Startup.cs:

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

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });

Here is the controller in the folder \Areas\Admin\Controllers:

namespace MyWebSite.Areas.Admin.Controllers
{
    [Area("Admin")]
    public class AdminController : Controller
    {
        private readonly ILogger _logger;

        public AdminController(ILogger<AdminController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            _logger.LogInformation("GET: Admin/Index/");
            return View();
        }

        public IActionResult aa()
        {
            _logger.LogInformation("GET: Admin/aa/");
            return View();
        }

        public IActionResult bb()
        {
            _logger.LogInformation("GET: Admin/bb/");
            return View();
        }
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

As the pattern setting in your startup.cs:

pattern: "Admin/{controller=Home}/{action=Index}/{id?}");

This means that your route must start with Admin(area name), followed by the controller name and action name.

The controller name in your current Admin area is also Admin, so if you want to see the corresponding page, you should enter the link:

http://localhost:xxxx/Admin/Admin/
http://localhost:xxxx/Admin/Admin/aa
http://localhost:xxxx/Admin/Admin/bb

If you want to follow your ideal route, you need to set it up like this:    

endpoints.MapAreaControllerRoute (
                   name: "AdminArea",
                   areaName: "Admin",
                   pattern: "{controller=Home}/{action=Index}/{id?}");

But this will ignore your Area name and just display http://localhost:xxxx/Controller/Action.

If you create another controller named HomeController under Admin area, your path will become: http://localhost:xxxx/Home/

Here is the test result:

enter image description here

LouraQ
  • 6,443
  • 2
  • 6
  • 16