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 ?
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.
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" forCompany
. 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 forCompany
?
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();
}
}
}