我需要帮助填充此端点文档的主体参数。它显示为空:
我意识到发生这种情况的原因是因为我目前正在阅读请求正文,然后将信息转换为 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)