[英]How to generate Body Parameters/ Sample Request for Web API Help Page while having access to raw request body
I need help populating the Body Parameters for the documentation of this endpoint.我需要帮助填充此端点的文档的正文参数。 It shows as empty:它显示为空:
I realize that the reason this is happening is because I am currently reading the request body and then converting the information to PackageUpdate.我意识到发生这种情况的原因是因为我目前正在读取请求正文,然后将信息转换为 PackageUpdate。 Since I am reading the raw request body this way, the task has no parameters which will cause the help page to not specify any.由于我以这种方式读取原始请求正文,因此该任务没有将导致帮助页面未指定任何参数的参数。
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
}
How can I possibly get the Body Parameters field on the documentation to reflect the properties of PackageUpdate, and still have access to the raw request body?我怎样才能获得文档上的 Body Parameters 字段以反映 PackageUpdate 的属性,并且仍然可以访问原始请求正文?
I previously tried the below solution from a different post but it wouldn't allow me access to the request body as a string.我之前在另一篇文章中尝试了以下解决方案,但它不允许我以字符串形式访问请求正文。
public async Task<IHttpActionResult> UpdatePackageAsync([FromBody] PackageUpdate request)
I ended up trying multiple solutions that I found on the site and then found a solution ( here ) that allows me to seek to the beginning of the stream, copy to a new stream, and return the body to my Task.我最终尝试了我在网站上找到的多个解决方案,然后找到了一个解决方案(这里),它允许我寻找流的开头,复制到新流,并将正文返回到我的任务。
Solution:解决方案:
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());
}
Applicable to my code, I used it in this way:适用于我的代码,我是这样使用的:
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());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.