简体   繁体   中英

How to route in ASP.NET Core for deleting error 404 Not found

I reorganized my project folders, and I think It could be for the new "Customer area". In visual studio 2019 for mac, it doesn't appear to add area , not even in new scaffolding, so I added new folders.

I think the error 404 is because of "routing". I show my folders and code:

在此处输入图像描述

Controller:

    namespace BulkyBook.Areas.Customer.Controllers
{
    [Area("Customer")]
    //[Route("Customer/Home/Index/id")]
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

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

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}

Startup:

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

Thanks for helping.

You've got two issues. First, you've made id a required route param, so no route without that part will match anything. Second, you've made it so where only areas can be routed to. In other words, if there's not an area portion of the URL, then it will fail. The docs tell you how to do all this, and specifically, this is the route config you should have:

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

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

    // other endpoints, e.g.
    endpoints.MapRazorPages();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM