I have a pretty standard WebApi that does some basic CRUD operations.
I'm trying to add some different kind of lookups but am not quite sure how it's suppose to be done.
Here's my FoldersController currently
public class FoldersController : ApiBaseController
{
//using ninject to pass the unit of work in
public FoldersController(IApiUnitOfWork uow)
{
Uow = uow;
}
// GET api/folders
[HttpGet]
public IEnumerable<Folder> Get()
{
return Uow.Folders.GetAll();
}
// GET api/folders/5
public Folder Get(int id)
{
return Uow.Folders.GetById(id);
}
// POST api/folders
public HttpResponseMessage Post(Folder folder)
{
Uow.Folders.Add(folder);
Uow.Commit();
var response = Request.CreateResponse(HttpStatusCode.Created, folder);
// Compose location header that tells how to get this Folder
response.Headers.Location = new Uri(Url.Link(WebApiConfig.DefaultRoute, new { id = folder.Id }));
return response;
}
// PUT api/folders
public HttpResponseMessage Put(Folder folder)
{
Uow.Folders.Update(folder);
Uow.Commit();
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
// DELETE api/folders/5
public HttpResponseMessage Delete(int id)
{
Uow.Folders.Delete(id);
Uow.Commit();
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
what I would like to do is add a method that looks something like this
public IEnumerable<Folder> GetChildFolders(int folderID)
{
return Uow.Folders.GetChildren(folderID);
}
Since I already have the standard Get method in there, i'm not quite sure how to do so.
I initially thought I could just add a new route..something like
routes.MapHttpRoute(
name: "ActionAndIdRoute",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: null,
constraints: new { id = @"^/d+$" } //only numbers for id
);
And the just add something like an ActionName annotation to my method [ActionName("GetChildren")]
but that didn't fly.
Am I on the right track? How do I do something like this without adding another controller?