I need to save my request and response JSON in database and so far after i didn't find any code to do so.
In older .net core version i used below code to read body :
context.Request.EnableRewind();
But it seems this code not working in .Net 6 or i don't know the trick to use it, Here it is my last code which is able to read response but request is always empty.
public class APILogFilter : IActionFilter
{
public async void OnActionExecuted(ActionExecutedContext context)
{
string RequestBody = "";
context.HttpContext.Request.EnableBuffering();
using (StreamReader reader = new StreamReader(context.HttpContext.Request.Body, Encoding.UTF8, true, 1024, true))
{
RequestBody = await reader.ReadToEndAsync();
};
string ResultBody = "";
if (context.Result is ObjectResult)
{
var objResult = (ObjectResult)context.Result;
ResultBody = HelperConvert.GetJSONSerialize(objResult.Value);
}
//Save request and response in database
}
public void OnActionExecuting(ActionExecutingContext context)
{
}
}
I hope someone can help me out.