3.12设置原始 raw 字符串内容

创建于 2026 年 8 月 17 日约 1 分钟读完

在诸如 Postman 等现代 API 测试工具中,用户可以通过 raw 数据格式发送请求。在 ASP.NET Core 服务端应用程序中,这通常表现为接收一个标记有 [FromBody] 特性的字符串参数(例如 str):

cs
[HttpPost]// [Consumes("application/json")]public string AddBodyString([FromBody] string str)  // 注意:默认情况下 ASP.NET Core 不支持 text/plain 内容类型的绑定{    return str;}

为了设置原始字符串内容,可以采用如下方法:

cs
HttpRequestBuilder.Post("https://furion.net/")    .SetRawStringContent("Furion"); // 默认内容类型 text/plainHttpRequestBuilder.Post("https://furion.net/")    .SetRawStringContent("Furion", "application/json"); // 内容类型必填HttpRequestBuilder.Post("https://furion.net/")    .SetContent("\"Furion\"", "application/json"); // 等价 SetRawStringContent 方式调用