English 中文(简体)
MVC:RESTful routing with nested urls, when entities are not really nested
原标题:

I m really having a hard time with the RESTful paradigm + nested urls. I have asked a question that lead me to where I am right now here. My Domain is roughly this: there are schools, courses of a school and teachers of schools. Now schools, courses and teacher are not "nested" entities in the sense that you can refer to any of them with one Id. The site is a collection of "micro-sites" for each school showing each one s courses and teachers. A course or teacher can only exist in one school.

say we have a url like /schools/1/courses/10 . Course 10 of school 1. This works fine. Say that the user changes by hand 10 into 11, which happens to exist but is a course of school 2. Right now that leads to a mess, my site still "thinks" the user is in school 1 but shows course 3 as part of it.

Should I make detail-actions parametric to both the Id being asked for AND the "parent" entity (the school in this case) that it involved? Fetch from repositories not only by id but with a school constraint?

Or is there any better way to do this?

最佳回答

The way I would think about it is this; even though there may be a course 11 in your service, there is no resource that exists at the URI /schools/1/courses/11. Since there is no resource at this URI, I would return an HTTP 404 response to requests for the URI.

One way you may improve your service would be to replace the integer ID values with the names of the entities (this would require the names be unique). This would make your URIs more user friendly. An example would be /schools/Hogwarts/courses/Potions.

To further improve your service, you ll want to give users a way to navigate through the service to all of the different resources available. For example you ll probably want to allow them get a list of all courses offered by a certain school. To do that you d expose a resource at /schools/Hogwarts/courses/ whose return type would be a list of all courses offered by the school. The representation of this list could be an XML document like the following snippet:

<courses>
    <course uri="/schools/hogwarts/courses/defense+against+the+dark+arts">Defense against the dark arts</course>
    <course uri="/schools/hogwarts/courses/potions">Potions</course>
</courses>
问题回答

Should I make detail-actions parametric to both the Id being asked for AND the "parent" entity (the school in this case) that it involved? Fetch from repositories not only by id but with a school constraint?

Yes.

Or is there any better way to do this?

Nothing wrong with what you described above.

You could do it with a catch-all route, and then parsing the url elements yourself.





相关问题
Is it possible to redirect content calls in ASP.NET?

I m experimenting with javascript and css caching in ASP.NET MVC. Is it possible to intercept calls to the server for these types of files? For example, if a request gets to the server for ~/...

Mixing ASP.NET MVC into ASP.NET WebForms

For some reason my routing is ignoring any attempt to access my MVC pages and simply giving me 404s. I have a WebForms app set up like the following: Virtual Directory: thing So I usually access ...

ASP.Net MVC RouteData and arrays

If I have an Action like this: public ActionResult DoStuff(List<string> stuff) { ... ViewData["stuff"] = stuff; ... return View(); } I can hit it with the following URL: http://...

Gradually migrating Classic ASP 3.0 to ASP.NET MVC

I m evaluating a migration from a classic ASP 3.0 application to ASP.NET MVC. I ve already migrated applications to WebForms, but have decided to try MVC for this migration for a number of reasons, ...

热门标签