Reputation: 2945
I'd like to get some advice about how to set up the controllers for a new site.
This site will have Libraries within locations, and each library can have a set of books.
The URL structure will look like this:
/Library-Name/Books/Book-Name
The books may also have categories or genres:
/Library-Name/Category/Books/Book-Name
My question is, do I create just a Library controller with all the logic in there of adding/removing libraries as well as adding and removing books?
Or do I create a libraries controller and a books controller? If so how do I ensure that books are 'tied' to a specific library?
Sorry for the simple question, but none of the main tutorials seem to have a nested route like this.
Thanks, Robbie
Upvotes: 1
Views: 126
Reputation: 281
LibraryName (maybe) makes sense (but some obvious location issues pop up), if you're using multiple physically separate libraries?
Note, you'll probably start to hit Routing hell with this method, but it's not impossible, so I'd research and define your structure first.
You would probably be better with something like the following URI structure:
/libraries/{LibraryName}/books/
Get a list of books in libraryName
/libraries/{LibraryName}/books/{BookISBN}
Get a book by it's ISBN number
/libraries/{LibraryName}/books?category=somecategory
Get a list of books in somecategory
With an Example route of:
routes.MapRoute(
name: "LibraryBooks",
url: "libraries/{id}/books",
defaults: new { controller = "Libraries", action = "GetBooks" }
);
And it's method in a LibrariesController:
public IEnumerable<IBook> GetBooks(string id)
{
return new IBook[] { new Book(), new Book() };
}
Upvotes: 2
Reputation: 1027
You want to display books, so you need a BookController and not a LibraryController in my opinion. The BookController fetches a specific book (or list of books) based on the library and / or category that are passed to the BookController as parameters.
The logic to retrieve data should be placed in a separate class library if you want to set things up properly (divide UI, business logic, data access)
Upvotes: 0
Reputation: 2005
Check out Areas.
If you're going to have code that is repeated in multiple controllers, consider creating separate classes and methods for that code so that you can follow the DRY principle.
Upvotes: 2