有没有办法在 C# 中使用一个 httpget 请求来处理多个查询字符串参数?

Is there a way to use one httpget request to handle multiple query string parameters in c#?

我是 c# 的新手,并且在这方面使用控制器。下面的代码通过使用 api/Accounts?mgmId=00u100gp4rDgSjGup1t7

查询参数来工作
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Accounts>>> GetAccountDataAsync(string? mgmId)
        {
            try
            {
                string query = $"SELECT * FROM c WHERE c.MGMId=@mgmId";
                QueryDefinition queryDefinition = new QueryDefinition(query).WithParameter("@mgmId", mgmId);
                var account = await _accountService.GetAccountDataAsync(queryDefinition);
                return Ok(account);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return StatusCode(404, "Not Found");
            }
        }

我想做的是传递如下内容,这样我就可以在 CosmosDb 数据库中通过任何键进行查询,而不仅仅是 mgmId。

        [HttpGet]
        public async Task<ActionResult<IEnumerable<Accounts>>> GetAccountDataAsync(Accounts acc)
        {
            try
            {
                string query = $"SELECT * FROM c WHERE c.MGMId=@mgmId";
                QueryDefinition queryDefinition = new QueryDefinition(query).WithParameter("@mgmId", acc.MGMId);
                var account = await _accountService.GetAccountDataAsync(queryDefinition);
                return Ok(account);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
                return StatusCode(404, "Not Found");
            }
        }

我把我的Accounts.cs放在下面

    public class Accounts
    {
        [JsonPropertyName("id")]
        public string? Id { get; set; }
        [JsonPropertyName("MGMId")]
        public string MGMId { get; set; } = null!;
        [JsonPropertyName("MlifeId")]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public long MlifeId { get; set; } = 0!;
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public long? OperaId {get; set;}
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
        public object? Profile {get; set;}
        
    }

这是我的 AccountsService

        public async Task<IEnumerable<Accounts>> GetAccountDataAsync(QueryDefinition queryString)
        {
            var query = this._container.GetItemQueryIterator<Accounts>(queryString);
            List<Accounts> results = new List<Accounts>();
            while (query.HasMoreResults)
            {
                var response = await query.ReadNextAsync();

                results.AddRange(response.ToList());
            }
            return results;
        }

这是我在尝试通过模型绑定将帐户作为参数传递时遇到的错误: “错误”:{ “$”:[ “输入不包含任何 JSON 标记。当 isFinalBlock 为真时,预期输入以有效的 JSON 标记开头。路径:$ | LineNumber:0 | BytePositionInLine:0。” ], “acc”:[ “acc 字段是必需的。” ]

关于您的场景,您需要添加 FromQuery 属性来获取 GET 操作的数据:

[HttpGet]
public async Task<ActionResult<IEnumerable<Accounts>>> GetAccountDataAsync([FromQuery]Accounts acc)

您可以在 MSDN 上阅读有关参数绑定的更多信息。