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/a@a.com -get a particular author for a book PUT /books/1/authors/a@a.com - update a particular author for a book DLETE /books/1/authors/a@a.com -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?
I am confused how to add a route to make this call work. By default I get this route with the project.
Here is one approach you may consider,
public class BooksController : ApiController
{
public string GetBooks()
{
return "Books: ";
}
public string GetBook(int id)
{
return "Books: "+id.ToString();
}
public string GetAuthors(int id, string sub)
{
return "Books: "+id.ToString()+" Authors: ";
}
public string GetAuthor(int id, string sub, int subid)
{
return "Books: "+id.ToString()+" Authors: " + subid.ToString();
}
}
.................................................
.................................................
.................................................
static Dictionary<string, string> subResources = new Dictionary<string, string>();
private static void FillResources()
{
subResources.Add("authors", "books");
}
public class SubResourceConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (values[parameterName] == null)
return true;// no sub resource found
var sub = values[parameterName].ToString();
if (string.IsNullOrWhiteSpace(sub))
return true;// no sub resource found
string output;
return subResources.TryGetValue(sub, out output);
}
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{sub}/{subid}",
defaults: new { id = RouteParameter.Optional, sub = RouteParameter.Optional, subid = RouteParameter.Optional },
constraints: new { id = @"\d*", subid = @"\d*", sub = new SubResourceConstraint() }
);
..............................................................................
..............................................................................
..............................................................................
protected void Application_Start()
{
FillResources();
..............................................................................
..............................................................................
..............................................................................
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
msskiran
0 Points
3 Posts
Handle collections inside collections in Web API using ASP .Net MVC4
May 22, 2012 09:30 PM|LINK
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:
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:
I am confused how to add a route to make this call work. By default I get this route with the project.
What is the right way to handle routes in this pattern for collections and associations between them?
- Shashikiran
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: Handle collections inside collections in Web API using ASP .Net MVC4
May 23, 2012 05:04 AM|LINK
Here is one approach you may consider,
public class BooksController : ApiController { public string GetBooks() { return "Books: "; } public string GetBook(int id) { return "Books: "+id.ToString(); } public string GetAuthors(int id, string sub) { return "Books: "+id.ToString()+" Authors: "; } public string GetAuthor(int id, string sub, int subid) { return "Books: "+id.ToString()+" Authors: " + subid.ToString(); } } ................................................. ................................................. ................................................. static Dictionary<string, string> subResources = new Dictionary<string, string>(); private static void FillResources() { subResources.Add("authors", "books"); } public class SubResourceConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { if (values[parameterName] == null) return true;// no sub resource found var sub = values[parameterName].ToString(); if (string.IsNullOrWhiteSpace(sub)) return true;// no sub resource found string output; return subResources.TryGetValue(sub, out output); } } public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}/{sub}/{subid}", defaults: new { id = RouteParameter.Optional, sub = RouteParameter.Optional, subid = RouteParameter.Optional }, constraints: new { id = @"\d*", subid = @"\d*", sub = new SubResourceConstraint() } ); .............................................................................. .............................................................................. .............................................................................. protected void Application_Start() { FillResources(); .............................................................................. .............................................................................. ..............................................................................Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
msskiran
0 Points
3 Posts
Re: Handle collections inside collections in Web API using ASP .Net MVC4
Jun 22, 2012 07:00 PM|LINK
Thanks for the reply. I was using MVC4 Beta. Now with MVC4 RC out, and a new design, this solved my problem. Your idea gave inputs to me. Thanks!