0

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.

Noel
  • 13
  • 4
  • check the order of how you register the routes. The current order is causing route conflicts as the first one is consuming all the requests – Nkosi Apr 03 '18 at 16:15
  • 1
    See [Why map special routes first before common routes in asp.net mvc?](https://stackoverflow.com/a/35674633) – NightOwl888 Apr 03 '18 at 19:22
  • Thanks for your responses. I was able to resolve this. I am posting my resolution below as an answer to the above-mentioned specific question. – Noel Apr 04 '18 at 10:06

1 Answers1

0

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

Noel
  • 13
  • 4