1

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.

8
  • 1
    The routes only match the path, not the query. You should apply the RESTful and represent your resources via urls. i.e. http://localhost/api/manager/2/item Commented Jan 30, 2018 at 13:07
  • 1
    I agree with Tseng. But that said, your code should work. HTTP 500 suggests an error. Were you running the debugger when that happened? Did you see an exception? Also, the error page should tell you an error message. Commented Jan 30, 2018 at 13:58
  • 1
    @GabrielLuci is correct. The code you've posted here will not generate a 500, so the only assumption is that what you've posted is not an accurate representation of the code you're actually running. Perhaps you redacted too much. Regardless, please create a complete, minimal and reproduceable sample. Currently, you do not have that. Commented Jan 30, 2018 at 14:19
  • Hi @Tseng , thanks for the advice. For this moment, we can't change the URL to RESTful style because there is a deadline in near future. Commented Jan 31, 2018 at 5:03
  • Hi @GabrielLuci, the web api is actually hosted in Azure Service Fabric (although it's hosted in local cluster), for some reason, everytime I start the service fabric project (which refers the web api project) with 'Start Debugging' in Visual Studio, VS throws out an error message "A fatal error has occurred....". We know this error is caused by the projects upgraded to .NET Core 2.0 but don't know why. That's why I mentioned I can't debug it. Commented Jan 31, 2018 at 5:22

1 Answer 1

1

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();

    }


}

Sign up to request clarification or add additional context in comments.

1 Comment

Doing it like that you can get some benefits and one of them is that you can establish some priority to the query params.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.