inside
inside

Reputation: 3177

Multiple controllers specified in one route

In our ASP.NET MVC architecture we have the following routing structure:

http://localhost:80/Students/Details/2 - will give me details about student with id == 2, details include all the classes current student have.

http://localhost:80/Classes/Details/2 - will give me details about classes with id == 2, details include all the students current class have.

http://localhost:80/Schedule/Details/class=2&student=2 - will give me details about specific schedule for student with id=2 that has class with id=2

This works fine so far.

Now, our team is pretty new to asp.net mvc and we were thinking about making routing a bit more intuitive. So that instead of three separate route we will have one long route, something like this:

http://localhost:80/Students/2/Classes/2/Homework/ 

and it will work like:

http://localhost:80/Students/ - will give list of students
http://localhost:80/Students/2 - will give details about student 2
http://localhost:80/Students/2/Classes - will give all the classes for student with id 2
http://localhost:80/Students/2/Classes/2 - will give schedule for class with id 2 and student with id 2. 

Not quit sure if that's reasonable/possible, just wanted to get more opinions

Upvotes: 1

Views: 1334

Answers (1)

zed
zed

Reputation: 2338

MVC Routes are mapped to a single action inside a controller class.
You could configure a route for every case, mapping to a different action on each one. Remember that routes should be placed from more specific to more generic.

In your App_Start/RouteConfig.cs:

        routes.MapRoute(
            name: "StudentSchedule",
            url: "students/{studentID}/classes/{classID}",
            defaults: new { controller = "Home", action = "StudentSchedule" }
        );
        routes.MapRoute(
            name: "StudentClasses",
            url: "students/{studentID}/classes",
            defaults: new { controller = "Home", action = "StudentClasses" }
        );
        routes.MapRoute(
            name: "StudentDetails",
            url: "students/{studentID}",
            defaults: new { controller = "Home", action = "StudentDetails" }
        );
        routes.MapRoute(
            name: "StudentsList",
            url: "students",
            defaults: new { controller = "Home", action = "Students" }
        );

So, your actions will look like this, you will have to add the code to get data from database, you could share the code on sepparated classes/layers of your app:

    public ActionResult Students()
    {
        var viewmodel = new StudentsViewModel();
        ...
        return View(viewmodel);
    }

    public ActionResult StudentDetails(int studentID)
    {
        var viewmodel = new StudentDetailsViewModel();
        ...
        return View(viewmodel);
    }

    public ActionResult StudentClasses(int studentID)
    {
        var viewmodel = new StudentClassesViewModel();
        ...
        return View(viewmodel);
    }

    public ActionResult StudentSchedule(int studentID, int classID)
    {
        var viewmodel = new StudentScheduleViewModel();
        ...
        return View(viewmodel);
    }

Upvotes: 2

Related Questions