0

I am passing through quite a similar challenge to the one reported on this post - .Net Core API Endpoint not allowing QueryString parameters - but the accepted answer hasn't worked for me, so I am seeking some guidance.

    [HttpGet, Route("api/indicators/getindicatorsvalues/{companyId=companyId}/{pathToFile=pathToFile}")]
    [ProducesResponseType(typeof(ComputedIndicatorVM), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetIndicatorsValues([FromQuery] Guid companyId, [FromQuery] string pathToFile)
    {
       //code goes here
    }

    [HttpGet("{id}")]
    [ProducesResponseType(typeof(IndicatorDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetByIdAsync(Guid id)
    {
        //some more code goes here
    }

Calling the 1st endpoint:

URL: https://localhost:5001/api/indicators/GetIndicatorsValues?companyId=cTest&pathToFile=ptfTest

Result: {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"|6c8dcccd-412c0e1f0b9eb222.","errors":{"id":["The value 'GetIndicatorsValues' is not valid."]}}

Calling the 2nd endpoint works just fine:

URL: https://localhost:5001/api/indicators/DFAF6EAE-AB4B-4563-B37E-57DEF730A1D7

It seems by the response of the first endpoint that it is considering GetIndicatorsValues as a param for the second endpoint? Or am I missing something else?

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
AMFerreira
  • 23
  • 4

2 Answers2

0

According to your description, it seems this issue is the https://localhost:5001/api/indicators/GetIndicatorsValues will match both the GetIndicatorsValues and GetByIdAsync. I guess your has a default rule which is like {controller}/{action}/{id?}.

I suggest you could try to modify the controller codes to avoid match the same method and then it will work well.

[HttpGet, Route("api/indicators/getindicatorsvalues/{companyId=companyId}/{pathToFile=pathToFile}")]
[ProducesResponseType(typeof(ComputedIndicatorVM), StatusCodes.Status200OK)]
public async Task<IActionResult> GetIndicatorsValues([FromQuery] Guid companyId, [FromQuery] string pathToFile)
{
   //code goes here
}

[HttpGet("api/indicators/getindicatorsvalues/GetByIdAsync/{id}")]
[ProducesResponseType(typeof(IndicatorDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetByIdAsync(Guid id)
{
    //some more code goes here
}
Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • Thank you Brando for your suggestion, but that hasn't worked. GetByIdAsync stopped working and GetIndicatorsValues still doesn't work. I will keep exploring and will post the solution here once I get it. – AMFerreira Dec 13 '21 at 22:04
0

Sorted:

    [HttpGet]
    [ProducesResponseType(typeof(IndicatorDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> GetByIdAsync(Guid id)
    {

        var indicator = await this.indicatorsService.GetByIdAsync(id);
        if (indicator == null)
        {
            return NotFound();
        }

        return Ok(indicator);
    }

    [HttpGet("GetIndicatorsValues")]
    [ProducesResponseType(typeof(ComputedIndicatorVM), StatusCodes.Status200OK)]
    public async Task<IActionResult> GetIndicatorsValues([FromQuery] Guid companyId, [FromQuery] string pathToFile)
    {
        try
        {
            if (companyId!=default && !string.IsNullOrEmpty(pathToFile))
            {
                List<ComputedIndicatorVM> computedIndicatorsViewModel = new List<ComputedIndicatorVM>();
                List<ComputedIndicatorDto> dtoList = await this.indicatorsService.CalculateIndicators(companyId, pathToFile);
                computedIndicatorsViewModel = this.mapper.Map<List<ComputedIndicatorVM>>(dtoList);
                return Ok(computedIndicatorsViewModel);
            }
            else
            {
                return NotFound();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
            throw ex;
        }
    }
AMFerreira
  • 23
  • 4