I am kinda blocked with some routing issues in my ASP.NET MVC application.
Let us assume I have 2 controllers which are:
- TaskList Controller
- 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.