當我們需要取得httRequest的原始body內容,不是透過mvc的modelBinding
可以寫一個httRequest的extension
public static class HttpRequestExtension
{
public static async Task<string> ReadBodyAsync(this HttpRequest request)
{
using (var reader = new StreamReader(request.Body))
{
var body = await reader.ReadToEndAsync();
if (request.Body.CanSeek) request.Body.Seek(0, SeekOrigin.Begin);
return Regex.Replace(body, "[\r\n\t]", string.Empty, RegexOptions.Compiled);
}
}
}
在mvc的controller內, 就可以使用
Request.ReadBodyAsync()
來取得傳入的body內容