views:

29

answers:

1

I am implementing a very simple requirements management tool.

I want the URLs to look like this:

Shows home page for "Project One":
http://projectmanager/Project/Project%20One

Shows a list of requirements being worked on for "Project One"
http://projectmanager/Project/Project%20One/Requirements

Shows requirement 1 for "Project One"
http://projectmanager/Project/Project%20One/Requirement/1

How could I set up routes so that

http://projectmanager/Project/Project%20One 

is handled by the project controller

http://projectmanager/Project/Project%20One/Requirements
and
http://projectmanager/Project/Project%20One/Requirements/1

is handled by the requirements controller.

Is it even possible?

A: 

I think it is.

One of the options:

Controllers:

public class ProjectController : Controller
{
    public ActionResult Project(string projectName)
    {
        return Content("Project: " + projectName);
    }
}

public class RequirementsController : Controller
{
    public ActionResult Requirements(string projectName)
    {
        return Content("Requirements: " + projectName);
    }

    public ActionResult Requirement(string projectName, int id)
    {
        return Content("Requirement: " + projectName + " - " + id);
    }
}

Route registration:

        routes.MapRoute(null,
                        "Project/{projectName}",
                        new {controller = "Project", action = "Project"});
        routes.MapRoute(null,
                        "Project/{projectName}/Requirements",
                        new { controller = "Requirements", action = "Requirements" });
        routes.MapRoute(null,
                        "Project/{projectName}/Requirement/{id}",
                        new { controller = "Requirements", action = "Requirement" });

Id depends what other routes do you want.

Edit: by the way: this routes should be added before your default route (if you still have it from the app template).

rrejc