locked
Unable to go to the default route RRS feed

  • Question

  • User-389597101 posted

    Hi 

    I am new to asp.net core. I need some help. In the startup.cs, I have used this code

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

                    endpoints.MapRazorPages();
                });

    But by default it is not going to http://localhost/Customer/Home. Even when I manually do that, I am getting an error -

    This localhost page can’t be found

    Tuesday, April 13, 2021 12:50 PM

Answers

  • User1686398519 posted

    Hi karang, 

    1. You need to Add Area route. At the same time, you need to add [Area("Area name")] attribute to the Controller in the Area.

    Controller

        [Area("Customer")]
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
        }

    Startup

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

    Here are some links that can help you.

    1. Area route
    2. Routing to controller actions in ASP.NET Core

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 14, 2021 4:29 AM

All replies

  • User-474980206 posted

    You specified the default area as a constraint. Try

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

    Tuesday, April 13, 2021 2:29 PM
  • User1686398519 posted

    Hi karang, 

    1. You need to Add Area route. At the same time, you need to add [Area("Area name")] attribute to the Controller in the Area.

    Controller

        [Area("Customer")]
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                return View();
            }
        }

    Startup

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

    Here are some links that can help you.

    1. Area route
    2. Routing to controller actions in ASP.NET Core

    Best Regards,

    YihuiSun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 14, 2021 4:29 AM
  • User-389597101 posted

    Thank you YihuiSun 

    Wednesday, April 14, 2021 9:30 AM