如何在访问原始请求正文的同时为 Web API 帮助页面生成正文参数/示例请求

How to generate Body Parameters/ Sample Request for Web API Help Page while having access to raw request body

我需要帮助填充此端点文档的正文参数。它显示为空:

我意识到发生这种情况的原因是因为我当前正在读取请求正文,然后将信息转换为 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

        }

我怎样才能获得文档中的主体参数字段以反映 PackageUpdate 的属性,并且仍然可以访问原始请求主体?

我以前从另一个 post 尝试过以下解决方案,但它不允许我以字符串形式访问请求正文。

public async Task<IHttpActionResult> UpdatePackageAsync([FromBody] PackageUpdate request)

我最终尝试了在网站上找到的多种解决方案,然后找到了一个解决方案 (here),它允许我搜索到流的开头,复制到新的流,然后 return 正文到我的任务。

解法:

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());
      }
}