.NET 8 Blazor WASM客户端无法向Web API正确发送数据
问题:Blazor WASM及WinForms客户端无法向Web API传递textContent字段
我的Web API在Postman和其他两个开发客户端中运行正常,但.NET 8 Blazor WASM和WinForms测试客户端无法正确传递数据。API日志显示请求已到达端点,但textContent字段始终被报告为空(实际已传入有效值)。已确认字段大小写与API的驼峰式参数完全匹配,调试输出的JSON格式正确无拼写错误,但问题依旧。API已上线无法修改。
错误信息:
'textContent' cannot be null or empty
排查后发现API支持multipart表单,Postman和Swagger能生成正确的POST请求格式,推测问题出在请求格式上,需要让HttpClient改用multipart表单发送请求。
解决方案:改用MultipartFormDataContent发送请求
API实际期望接收multipart/form-data格式的请求,而非JSON格式。即使JSON结构正确,API无法解析JSON请求体中的字段,因此需要将请求内容改为multipart表单形式。
修改后的Blazor服务代码(TextToAudioService.cs)
using System; using System.Net.Http; using System.Threading.Tasks; namespace TextToAudioClient.Services { public class TextToAudioService { private readonly HttpClient _httpClient; public TextToAudioService(HttpClient httpClient) { _httpClient = httpClient; } public async Task<string> ConvertTextToAudioAsync(string textContent, string outputFileNameNoExt, string audioFormat = "mp3", string voice = "echo", double speed = 0.9) { try { var formContent = new MultipartFormDataContent(); // 表单字段key需与API期望的驼峰式名称完全一致 formContent.Add(new StringContent(textContent), "textContent"); formContent.Add(new StringContent(outputFileNameNoExt), "outputFileNameNoExt"); formContent.Add(new StringContent(audioFormat), "audioFormat"); formContent.Add(new StringContent(voice), "voice"); formContent.Add(new StringContent(speed.ToString()), "speed"); var response = await _httpClient.PostAsync("api/v1/texttoaudio/convertfrombody", formContent); response.EnsureSuccessStatusCode(); var audioUrl = await response.Content.ReadAsStringAsync(); return audioUrl; } catch (HttpRequestException httpRequestException) { Console.WriteLine($"HTTP请求错误: {httpRequestException.Message}"); throw new ApplicationException("处理请求时发生错误。", httpRequestException); } catch (Exception ex) { Console.WriteLine($"错误: {ex.Message}"); throw new ApplicationException("发生意外错误。", ex); } } } }
修改后的WinForms测试客户端代码
using System.Net.Http; namespace TextToAudioClient; public partial class Form1 : Form { private readonly HttpClient _httpClient; public Form1() { InitializeComponent(); _httpClient = new HttpClient(); } private async void btnSendToAPI_Click(object sender, EventArgs e) { string requestUri = txtAPIroot.Text.Trim() + txtAPIendpointPath.Text.Trim(); var formContent = new MultipartFormDataContent(); formContent.Add(new StringContent(txtTextContent.Text.Trim()), "textContent"); formContent.Add(new StringContent(txtOutputFileNameNoExt.Text.Trim()), "outputFileNameNoExt"); formContent.Add(new StringContent(txtAudioFormat.Text.Trim()), "audioFormat"); formContent.Add(new StringContent(txtVoice.Text.Trim()), "voice"); formContent.Add(new StringContent(txtSpeed.Text.Trim()), "speed"); try { var response = await _httpClient.PostAsync(requestUri, formContent); response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStringAsync(); MessageBox.Show("收到响应: " + responseBody, "成功", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (HttpRequestException httpRequestException) { MessageBox.Show("HTTP请求错误: " + httpRequestException.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MessageBox.Show("发生错误: " + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
注意事项
- 表单字段的key(如
textContent)必须与API期望的参数名称完全一致,包括大小写 - 移除了原有的JSON序列化相关代码,因为API不接受JSON格式的请求体
MultipartFormDataContent会自动设置正确的Content-Type请求头,无需手动指定
内容的提问来源于stack exchange,提问作者MC9000
相关产品推荐
相关产品推荐

