user1408865
user1408865

Reputation: 171

Handle collections inside collections in Web API using ASP .Net MVC4

I installed the new ASP .Net MVC4 beta on my machine and have been trying to understand how the Web API works. I built a demo for a single collection (ex. books). I followed the example on the asp.net website. I implemented my own methods for posting to a collections i.e. adding a new book, getting all books, getting a particular book, updating a book and deleting a book record. All this works fine.

Ex:

POST /books - adds a new book
GET /books - gets all books
GET /books/1 - get a particular book
PUT /books/1 - update a particular book
DELETE /books/1 - delete a particular book

Now I want to add another collection inside the books collection, say authors and want to implement the same POST, PUT, GET and DELETE calls for the new collection

I want the new calls to be something like this:

POST /books/1/authors - add a new author to a book
GET /books/1/authors - gets all authors of a book
GET /books/1/authors/[email protected] - get a particular author for a book
PUT /books/1/authors/[email protected] - update a particular author for a book
DLETE /books/1/authors/[email protected] - delete a particular author for a book

I am confused how to add a route to make this call work. By default I get this route with the project.

routes.MapHttpRoute(
name: "DefaultApi", 
routeTemplate: "api/{controller}/{id}", 
defaults: new { id = RouteParameter.Optional }
);

What is the right way to handle routes in this pattern for collections and associations between them?

Upvotes: 4

Views: 489

Answers (2)

Ahmad Mushtaq
Ahmad Mushtaq

Reputation: 1395

I think Ken's method of using Attribute Routing is better, I just found about it from this post, and I myself probably will use that as well. But here is what I came up with before I knew about the AR.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "JustController",
            routeTemplate: "api/Books/{bookId}/{category}/{categoryId}/{subCategory}",
            defaults: new
            {
                controller = "books",
                bookId= RouteParameter.Optional,
                category = RouteParameter.Optional,
                categoryId = RouteParameter.Optional,
                subCategory = RouteParameter.Optional
            },
            constraints: new
            {
                bookId= @"\d*",
                category= @"(|authors|pictures|videos)", 
                categoryId = @"\d*",
                subCategory = @"(|comments)"
            }
        );

Then I was thinking of using the URL from the Request property in the Get, Post, Delete, Put functions, get the parameters I needed.

For example:

    public class BooksController : ApiController
    {
        // GET api/books
        public Book Get(int bookId)
        {
         var url = this.Request.RequestUri.ToString() // decide how to handle!

Upvotes: 0

Ken
Ken

Reputation: 844

Managing routes in Global can be confusing and error prone. Personally, I found the Attribute Routing package helps simplify routing configuration greatly. This article explains how to acquire and use it.

http://www.strathweb.com/2012/05/attribute-based-routing-in-asp-net-web-api/

Upvotes: 2

Related Questions