Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having multiple get-methods with multiple query string parameters in ASP.NET Core Web Api

I'm building a web api where I have one resourse that must have 3 get methods as follows:

    [HttpGet]
    [Route("{city}/{streetName}/{streetNumber}/{littera}")]
    public IActionResult GetByAddress([FromQuery]string city, [FromQuery]string streetName, [FromQuery]int streetNumber, [FromQuery]string littera)
    {
        var model = _availabilityService.FindByAddress(city, streetName, streetNumber, littera);
        return Ok(model);
    }

    [HttpGet("{pointId}")]
    public IActionResult GetByPointId(string pointId)
    {
        var model = _availabilityService.FindByPointId(pointId);
        return Ok(model);
    }

    [HttpGet]
    [Route("{xCoordinate}/{yCoordinate}")]
    public IActionResult GetByCoordinates([FromQuery]decimal xCoordinate, [FromQuery]decimal yCoordinate)
    {
        var model = _availabilityService.FindByCoordinates(xCoordinate, yCoordinate);
        return Ok(model);
    }

The get method with only one parameter(pointId) is working fine since it is not seen as a query string but rather and id. However the remaining 2 methods are not distinguishable by the router in ASP.NET, it seems.

I'm really at a loss here and cannot figure out why it doesn't work. What I have been able to work out is that if I remove one of the methods the other one works fine.

Any suggestions on what I'm doing wrong?

FYI, the corresponding url:s ought to look like the following:

api/1.0/availabilities?city=Metropolis&streetName=Superstreet&streetNumber=1&littera=A

and

/api/1.0/availabilities?xCoordinate=34.3444&yCoordinate=66.3422

Thanks!

like image 254
Kasper P Avatar asked Aug 03 '16 08:08

Kasper P


People also ask

Can we have multiple get methods in Web API?

As mentioned, Web API controller can include multiple Get methods with different parameters and types. Let's add following action methods in StudentController to demonstrate how Web API handles multiple HTTP GET requests.

How do I pass multiple parameters to Web API controller methods?

You can pass parameters to Web API controller methods using either the [FromBody] or the [FromUri] attributes. Note that the [FromBody] attribute can be used only once in the parameter list of a method.


1 Answers

First of all you are mixing RouteParameters and QueryParameters.

This:

[HttpGet]
[Route("{xCoordinate}/{yCoordinate}")]
public IActionResult GetByCoordinates([FromQuery]decimal xCoordinate, [FromQuery]decimal yCoordinate)
{
    var model = _availabilityService.FindByCoordinates(xCoordinate, yCoordinate);
    return Ok(model);
}

maps the controller action GetByCoordinates to a route like this:

/api/1.0/availabilities/34.3444/66.3422

But you are also specifying that you are expecting xCoordinate and yCoordinate to be bound from query parameters. So above url would match the action, but xCoordinate and yCoordinate would be bound to it's default values (in this case 0).

So to get your desired route, you shouldn't declare route parameters:

[HttpGet]
[Route("")] // <- no route parameters specified
public IActionResult GetByCoordinates([FromQuery]decimal xCoordinate, [FromQuery]decimal yCoordinate)
{
   // will be matched by e.g.
   // /api/1.0/availabilities?xCoordinate=34.3444&yCoordinate=66.3422
}

Now your desired route will match.

Note: You cannot map two actions to the same route - the route middleware wouldn't know which one to select. So also removing the route parameters from GetByAddress will effectively map both actions to the same route:

/api/1.0/availabilities?{any=number&of=query&parameters=here}

So you will have to differentiate them by another route segment for example.

[HttpGet]
[Route("address")] // <--
public IActionResult GetByAddress([FromQuery]string city, [FromQuery]string streetName, [FromQuery]int streetNumber, [FromQuery]string littera)
{
    // will be matched by e.g.
    // api/1.0/availabilities/address?city=Metropolis&streetName=Superstreet&streetNumber=1&littera=A
}

Further reading:

ModelBinding / Routing

Quick tip:

Set Microsft loglevel to Debug in appsettings.json (auto generated in standard Asp.Net Core WebApplication Template) and you will get very useful information on route selection / errors while route selection in your console output when running under kestrel.

{
  "Logging": {
  "IncludeScopes": false,
  "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Debug"
}

Or set up the debug logger in StartUp.cs to LogLevel.Debug and you get the same information in debug output directly in Visual Studio.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        // ...

        loggerFactory.AddDebug(LogLevel.Debug);

        // ...
    }
like image 149
ypsilo0n Avatar answered Sep 20 '22 14:09

ypsilo0n