asp.net-coreasp.net-mvc-routingasp.net-mvc-areas

Area endpoint routing in asp.net core 3.1


This is a simple question and I'm just asking out of curiosity. I've set up endpoint routing to area as such:

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

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

Here is the controller for the area home page:

[Area("mynewarea")]
[Route("mynewarea/[controller]/[action]")]
public class HomeController : Controller
{

    public IActionResult Index()
    {
        ViewBag.me = "hello world";
        return View();
    }
}

Here is the tag helper code in the view:

<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
<a class="nav-link text-dark" asp-area="mynewarea" asp-controller="Home" asp-action="Index">mynewarea</a>

This all totally works great but not consistently as I would expect. For example, if I put this in the URL

https://localhost/

it works as expected and I get to the home page. But if I put this in the URL

https://localhost/mynewarea

I get a 404.

However if I put this in the URL

https://localhost/mynewarea/Home/Index

I get my page as expected.

  1. Why do I have to be explicit in my area URLs?
  2. Is there a way to make default area URLs work implicitly, i.e., as a proper default URL?

Solution

  • was running aspnetcore v3.1.0
    when upgraded to current v3.1.1 problem went away