Search code examples
asp.net-coreasp.net-mvc-routingasp.net-mvc-areas

Area endpoint routing in asp.net core 3.1


this is a simple question and im just asking out of curiosity

ive 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();
        });

and here 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
so 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

so:
1. does anyone know why i have to be explicit in my area URLs
2. is there a way to make default area URLs work implicitly, ie as a proper default URL


Solution

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