Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

When exposing querystring parameters using GET I have the following base URL:

https://school.service.com/api/students

This will return the first 25 students.

What if I want to return a list of students based on one of the following criteria: - have accepted a job - have received a job offer - have no job offers

The three above choices are essentially an enum.

Therefore, the query request for students who have no job offers I assume would look like:

https://school.service.com/api/students?jobOfferStatus=3

However, I'm wondering if jobOfferStatus=3 is the proper way to handle this. If so, how would I publish/provide to the clients a list of available options for that jobOfferStatus query parameter? What about other possible query parameters and their valid options? We'll have many possible query parameters like this.

I'd love to see an example of how this should be done properly. What are the best practices?

share|improve this question

1 Answer

You have some options to do this, let's try to help:

1) Configure a generic route to asp.net web api knows how to solve another action's name different from Get to a get method, on the App_Start\WebConfigApi.cs class, try to add this:

config.Routes.MapHttpRoute("DefaultApiWithActionAndId",
            "api/{controller}/{action}/{id}",
            new { id = RouteParameter.Optional });

Using it, you can have diferent methods on the api controller:

// request: get
// url: api/Students/GetStudents
public HttpResponseMessage GetStudents() 
{
   return Request.CreateResponse(...);
}

// request: get    
// url: api/Students/GetStudentsWithJobOffer
public HttpResponseMessage GetStudentsWithJobOffer() 
{
   return Request.CreateResponse(...);
}

// request: get    
// url: api/Students/GetStudentsAcceptedJob
public HttpResponseMessage GetStudentsAcceptedJob() 
{
   return Request.CreateResponse(...);
}

2) Use a simple parameter on the Get method:

// request: get
// url: api/Students?jobOfferStatus=1       
public HttpResponseMessage GetStudents(int jobOfferStatus) 
{
    // use jobOfferStatus parameter to fill some list
   return Request.CreateResponse(...);
}

3) Use a simple method with a parameter named id, to get a default friendly url by asp.net mvc web api.

// request: get
// url: api/Students/1      
public HttpResponseMessage GetStudents(int id) 
{
    // use the id parameter to fill some list
   return Request.CreateResponse(...);
}
share|improve this answer
 
Thanks for the help, but what does this look like to the client? How does the client know about the extra methods? Typically, a client would only know about the 4 main HTTP verbs. Also, I might need to combine multiple query parameters in order to get a relevant result. –  Adam Levitt 6 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.