DotNet - API - 无法访问端点

DotNet - API - Can't access endpoint

我正在经历与关于此 post - 所报告的挑战非常相似的挑战 - 但公认的答案对我没有用,所以我正在寻求一些指导。

    [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
    }

调用第一个端点:

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

结果: {"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title": “发生了一个或多个验证错误。”,“status”:400,“traceId”:“|6c8dcccd-412c0e1f0b9eb222。”,“errors”:{“id”:[“值 'GetIndicatorsValues' 无效。 "]}}

调用第二个端点工作正常:

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

从第一个端点的响应来看,它似乎正在考虑将 GetIndicatorsValues 作为第二个端点的参数? 还是我漏掉了什么?

根据您的描述,这个问题似乎是 https://localhost:5001/api/indicators/GetIndicatorsValues 将匹配 GetIndicatorsValues 和 GetByIdAsync。我猜你有一个默认规则,就像 {controller}/{action}/{id?}.

我建议你可以尝试修改控制器代码,避免匹配相同的方法,然后它会很好地工作。

[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
}

已排序:

    [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;
        }
    }