如何在访问原始请求正文的同时为 Web API 帮助页面生成正文参数/示例请求
Posted
技术标签:
【中文标题】如何在访问原始请求正文的同时为 Web API 帮助页面生成正文参数/示例请求【英文标题】:How to generate Body Parameters/ Sample Request for Web API Help Page while having access to raw request body 【发布时间】:2020-04-28 14:12:38 【问题描述】:我需要帮助填充此端点文档的主体参数。它显示为空:
我意识到发生这种情况的原因是因为我当前正在读取请求正文,然后将信息转换为 PackageUpdate。由于我以这种方式读取原始请求正文,因此该任务没有任何参数会导致帮助页面未指定任何参数。
Sample Request:
"PackageId": "package/3e585e1c-d6cd-4b6c-aa1c-aa674d11c944",
"SoldDateTime": "2018-08-13 19:57:54.000",
"GuaranteeTermExpiresDate": null
PackageUpdate class:
[DataContract]
public class PackageUpdate: BaseRequest
/// <summary>
/// Get or Set the package ID of the package to update
/// </summary>
[Required]
[DataMember]
public string PackageId get; set;
/// <summary>
/// Get or Set the Sold Date Time field of the package
/// </summary>
[DataMember]
public DateTime? SoldDateTime get; set;
/// <summary>
/// Get or Set the Guarantee Term Expires Date field of the package
/// </summary>
[DataMember]
public DateTime? GuaranteeTermExpiresDate get; set;
/// <summary>
/// Get or Set the Premium Promised On field of the package
/// </summary>
[DataMember]
public DateTime? PremiumPromisedOn get; set;
UpdatePackageAsync Method:
/// <summary>
/// Updates a package.
/// </summary>
[Route("update"), Description("Patch a specific package")]
[HttpPatch]
[ResponseType(typeof(Package))]
public async Task<IHttpActionResult> UpdatePackageAsync()
string requestBody = await Request.Content.ReadAsStringAsync();
PackageUpdate request = new PackageUpdate();
try
JsonSerializerSettings settings = new JsonSerializerSettings MissingMemberHandling = MissingMemberHandling.Error ;
request = JsonConvert.DeserializeObject<PackageUpdate>(requestBody, settings);
catch(Exception e)
return BadRequest("Patch Error -> " + e.Message);
//Do stuff with request
我怎样才能获得文档中的 Body Parameters 字段以反映 PackageUpdate 的属性,并且仍然可以访问原始请求正文?
我之前在另一篇文章中尝试过以下解决方案,但它不允许我以字符串形式访问请求正文。
public async Task<IHttpActionResult> UpdatePackageAsync([FromBody] PackageUpdate request)
【问题讨论】:
对此问题有任何见解吗?如果我需要提供更清晰的信息,请告诉我。 【参考方案1】:我最终尝试了在网站上找到的多个解决方案,然后找到了一个解决方案 (here),它允许我寻找到流的开头,复制到新流,并将正文返回到我的任务.
解决方案:
using (var stream = new MemoryStream())
var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
context.Request.InputStream.Seek(0, SeekOrigin.Begin);
context.Request.InputStream.CopyTo(stream);
string requestBody = Encoding.UTF8.GetString(stream.ToArray());
适用于我的代码,我是这样使用的:
public async Task<IHttpActionResult> UpdatePackageAsync(PackageUpdate request)
string requestBody = ReadInputStream();
// do stuff with request
private string ReadInputStream()
using (var stream = new MemoryStream())
var context = (HttpContextBase)Request.Properties["MS_HttpContext"];
context.Request.InputStream.Seek(0, SeekOrigin.Begin);
context.Request.InputStream.CopyTo(stream);
return Encoding.UTF8.GetString(stream.ToArray());
【讨论】:
以上是关于如何在访问原始请求正文的同时为 Web API 帮助页面生成正文参数/示例请求的主要内容,如果未能解决你的问题,请参考以下文章
当内容类型为文本/纯文本时,.NET Core 1.0 Web Api 将请求正文处理为 JSON
如何在 Slim 中访问 POST 请求的 JSON 请求正文?