Asp.net MVC: Hierarchical Model and Domain Controllers

My domain model is this: we have a bunch of schools as the root of the "hierarchy". Each school has teachers and courses, and each course has one teacher. I am trying to simulate this with mvc structure logic and I am very confused. For example, \ school \ details \ x should provide the first page of the school. This should include a link to a list of their teachers and a list for each course.

The list of teachers means that the index action must be parametric for the school the user is looking at: \ teacher \ id, where id is the school. The same is with the course list. And then create a teacher or course must also be parametric in that the school we are looking at: \ teacher \ create \ x where x = school.

How can I carry my School ID with me? Is there some neat way to do this, or do I need to pass it all the time, to all the views it needs? It also makes the site URL very cryptic. I was thinking about a way to create a url structure like {school-alias}\{controller}\{action}\{id}

, but still I need to find a way to go through school. If this is the case, then I need to implement some kind of filter that will not allow the user to take certain actions if the school they are requesting does not match the one in their profile.

I suppose if I carry the schoolboy around the url, the site is more REST-like compared to, for example, getting the schoolId from the user's profile.

+2


source to share


3 answers


At the end, I will answer my own question.



Real solution: Restfull Routing . It implements the functionality in RoR, which is exactly what I need. It's a shame it's not a requirement of more people to be able to migrate to mvc-trunk.

0


source


I would create an abbreviation for each school. For example:

School Number. 1 - ABC School number. 2 - DEF

If I wanted to list the teachers, I would write

http: // site-address / ABC / teachers / list or just http: // site-address / ABC / teachers

Show basic information about the school

http: // site-address / ABC



Routing code:



    routes.MapRoute(
            "Default", // Route name
            "{acronym}/{controller}/{action}/{id}", // URL with parameters
            new {controller = "School", action = "Details", id = ""} // Parameter defaults
            );

      

code>

I would create an authorization action filter for teachers, school and controller class to check if the user has access to the school as defined by the acronym parameter in the url. You can check this by comparing the filterContext.RouteData.Values ​​["acronym"] file with the data stored in the profile.

+1


source


Write an extension method to overload the rendering of links that extract the school id (acronym or whatever you choose to use) from the routing data and add it to the route values ​​already traveled. This way, your action can choose to use an identifier if present, but you don’t need to add it to the view data, and you don’t need to forget to include it in any action links (you just need to remember that you are using an action link overload).

I would make the action reference overload quite obviously different so that someone after you can see that you are doing something out of the ordinary. It can be as simple as Html.SchoolActionLink (...).

For example: If your url is http://mydomain.com/abc/teachers/list and your route is defined as {school} / {controller} / {action} then the dictionary of route values ​​will have the value "abc" in the key "school ". Route values ​​can be accessed through HtmlHelper.ViewContext.RouteData.Values.

+1


source







All Articles