0

I got two controllers; ProjectController and DocumentController. One project can have several documents.

If I want to get documents by projectid, how and where should that be done using REST? (In general I havent got how REST deals with related data)

Edit: Found MVC Web API, get sub items that might be what I'm looking for.

Ivar

Community
  • 1
  • 1
Ivar
  • 215
  • 1
  • 6
  • 11

1 Answers1

1

I believe this should be done in the DocumentController. Below is an example

public class DocumentController : ApiController {

    public IQueryable<Document> GetDocs() 
    {

        //get db context 

            return db.documents;

    }

    public ICollection<Document> Get(int id)
    {
        return db.documents.where(d = d.ProjectId == id);

    }

}

The IQuerable version can be queried using odata filter query. like below:

http://server:port/api/Document/getdocs?$filter=ProjectId eq ‘1'
http://server:port/api/Document/getdocs?$filter=DocumentId eq ‘1001'

(you will have to modify the MapHttpRoute to register this route)

The get version (second function) will simply return collection based on the project id.

Abhijit-K
  • 3,569
  • 1
  • 23
  • 31