Search code examples
asp.net-mvcroutesurl-routing

ASP.NET MVC Routing not working well


I am kinda blocked with some routing issues in my ASP.NET MVC application.

Let us assume I have 2 controllers which are:

  1. TaskList Controller
  2. Task Controller

I'm not sure if this is overkill or not but I am aiming to have URL's as follows:

For TaskList Controller:

  • localhost:xxxx/tasklist/Create
  • localhost:xxxx/tasklist/
  • localhost:xxxx/tasklist/Details/1
  • localhost:xxxx/tasklist/Edit/1

For Task Controller:

  • localhost:xxxx/tasklist/1/Task/Create
  • localhost:xxxx/tasklist/1/Task
  • localhost:xxxx/tasklist/1/Task/Details/11
  • localhost:xxxx/tasklist/1/Task/Edit/11

I have set up my routing as follows:

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}",
            defaults: new { controller = "Home", action = "Index" }
        );

        routes.MapRoute(
            name: "TaskListRoute",
            url: "TaskList/{action}/{tasklistid}",
            defaults: new { controller = "TaskList", action = "Index", tasklistid = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "TaskRoute",
            url: "TaskList/{tasklistid}/{controller}/{action}/{taskid}",
            defaults: new { tasklistid = UrlParameter.Optional, controller = "Task", action = "Index", taskid = UrlParameter.Optional }
        );

Upon debugging the application, I am able to browse the TaskList controller with no problems but the moment I hit the following url on the Task Controller, I get a "Resource cannot be found" error:

http://localhost:xxxx/tasklist/1/Task

I have to type in the word "Index" like below in order for that page to work... http://localhost:xxxx/tasklist/1/Task/Index

The method signature behind the above url is...

public class TaskController : Controller
{
    // GET: Task
    public ActionResult Index(int tasklistid)
    {
        //Some code here....
    }
}

Any ideas where I wrong? Appreciate any advice.

Thanks in advance.


Solution

  • So after taking Nkosi's comment and NightOwl888's article into consideration all I had to do was modify the routing to look like the following:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.MapRoute(
                name: "TaskRoute",
                url: "TaskList/{tasklistid}/Task/{action}/{taskid}",
                defaults: new { controller = "Task", action = "Index", taskid = UrlParameter.Optional }
            );
    
            routes.MapRoute(
                name: "TaskListRoute",
                url: "TaskList/{action}/{tasklistid}",
                defaults: new { controller = "TaskList", action = "Index", tasklistid = UrlParameter.Optional }
            );
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}",
                defaults: new { controller = "Home", action = "Index" }
            );
        }
    

    Basically did the following:

    • Adjust routing to be in following order:

      1. TaskRoute
      2. TaskListRoute
      3. Default
    • In the TaskRoute, replaced '{controller}' with a literal like 'Task' which is actually the name of the controller.

    I hope this was the right thing to do.

    Cheers