You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor应用调用API时ID参数未传递的问题求助

排查思路与解决方案

排查步骤

  • 确认selectedBundle的实际值:在OnBundleSelect的try块开头添加提示(比如toastService.ShowInfo($"当前选中ID:{selectedBundle}")),验证该值是否为服务端预期的有效ID格式,而非空值或对象的默认字符串(比如如果res.Results是实体列表,是否误将整个实体赋值给selectedBundle而非实体的ID属性)。
  • 检查GetExecScript的实现逻辑:确认它是通过GET还是POST方式调用API,以及如何传递selectedBundle参数——若为GET请求,是否将ID拼入URL;若为POST请求,是否将参数正确传入SendRequestAsync的parameters参数。
  • 核对SendRequestAsync的参数处理逻辑:当前代码仅对POST请求处理parameters并放入请求体,若GetExecScript用的是GET请求,parameters不会被添加到URL的查询参数中,导致服务端无法接收。
  • 抓包验证请求:通过浏览器F12的Network面板查看第二个API的请求详情,确认URL中是否包含ID参数、或POST请求体中是否有参数数据,判断前端是否实际发送了参数。
  • 匹配服务端接收方式:确认服务端是从URL查询字符串、还是请求体中获取ID参数,前端请求方式需与服务端接收逻辑一致。

针对性解决方案

1. 修正GET请求的参数传递逻辑

如果GetExecScript使用GET请求,需要修改SendRequestAsync,将parameters转换为查询字符串拼入URL:

private async Task<T> SendRequestAsync<T>(string url, HttpMethod method, object? parameters = null)
{
    if (string.IsNullOrWhiteSpace(url))
    {
        throw new ArgumentException($"Parameter {nameof(url)} is required.");
    }

    var requestUrl = url;
    // 处理GET请求的参数
    if (method == HttpMethod.Get && parameters != null)
    {
        var queryBuilder = new QueryBuilder();
        foreach (var prop in parameters.GetType().GetProperties())
        {
            var value = prop.GetValue(parameters)?.ToString();
            if (!string.IsNullOrWhiteSpace(value))
            {
                queryBuilder.Add(prop.Name, value);
            }
        }
        requestUrl += queryBuilder.ToQueryString();
    }

    using var httpRequestMessage = new HttpRequestMessage(method, requestUrl);
    
    if (method == HttpMethod.Post && parameters != null)
    {
        httpRequestMessage.Content = JsonContent.Create(parameters,
            new MediaTypeHeaderValue("application/json"),
            new JsonSerializerOptions() { DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull });
        await httpRequestMessage.Content.LoadIntoBufferAsync();
    }

    var httpResponseMessage = await _HttpClient.SendAsync(httpRequestMessage);

    if (httpResponseMessage.StatusCode != System.Net.HttpStatusCode.OK)
    {
        throw new Exception($"请求错误,状态码:{httpResponseMessage.StatusCode},响应内容:{await httpResponseMessage.Content.ReadAsStringAsync()}");
    }

    var content = await httpResponseMessage.Content.ReadAsStringAsync();
    return Utilities.ParseContentToT<T>(content);
}

2. 确保selectedBundle是正确的ID值

如果res.Results是实体对象列表,而非字符串列表,需将实体的ID属性赋值给selectedBundle:

selectedBundle = res.Results.First().Id; // 替换为实际的ID属性名

3. 匹配服务端的参数接收方式

  • 若服务端从查询字符串取ID,GetExecScript需构造带参数的GET请求:
    public async Task<ExecScript> GetExecScript(string bundleId)
    {
        return await SendRequestAsync<ExecScript>("/api/execscript", HttpMethod.Get, new { id = bundleId });
    }
    
  • 若服务端从请求体取ID,GetExecScript需用POST请求,并确保参数结构与服务端模型一致:
    public async Task<ExecScript> GetExecScript(string bundleId)
    {
        return await SendRequestAsync<ExecScript>("/api/execscript", HttpMethod.Post, new { Id = bundleId });
    }
    

内容的提问来源于stack exchange,提问作者furybg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 16:12:04