Yang You
Yang You

Reputation: 2668

asp.net core web api - same url with different query parameter

I have a controller class as below:

[Route("api/[controller]")]
public class ItemController : Controller
{
    //GET api/item?employId=1009
    [HttpGet]
    public List<ItemViewModel> GetByEmployId([FromQuery]long employId) {
            return new List<ItemViewModel>() {
                new ItemViewModel(),
                new ItemViewModel(),
                new ItemViewModel()
            };
    }

    // GET api/item?managerId=1009
    [HttpGet]
    public List<ItemViewModel> GetByManagerId([FromQuery]long managerId) {
        return new List<ItemViewModel>();
    }
}

For the first URL (http://localhost/api/item?employId=1), the web api can return results. But for the second URL (http://localhost/api/item?managerId=2), the web api return error with HTTP code 500. Can someone help on why this happens? Thanks.

For some reason, I can't debug with the web api project.

Upvotes: 1

Views: 3208

Answers (1)

rubendmatos1985
rubendmatos1985

Reputation: 486

A bit outdated but just if someone has the same problem can find the response here. To achieve this you just have to add your queries params as function params. Using the same example.

[Route("api/[controller]")]
public class ItemController : Controller
{

    [HttpGet]
    [Route("")]
    public List<ItemViewModel> Get([FromQuery]long employId, [FromQuery] long managerId) {
            if(employId != null){
                    return new List<ItemViewModel>() {
                new ItemViewModel(),
                new ItemViewModel(),
                new ItemViewModel()
              };
             }
            if(managerId != null){

               return new List<ItemViewModel>();
           }

          return new List();

    }


}

Upvotes: 1

Related Questions