My scenario:
I'm using ASP .NET 5 Web API and I'm using content-type: application/json to post data to server.
Message:
Request headers
{...}
Content-Type:application/json
{...}
Request payload
{"Property1":"2280910","Property2":"734"}
MyController method:
[HttpPost]
public MyClassOutput GetDataRequest([FromBody] GetDataInput input) { ... }
I override the OnException method from the ExceptionFilterAttribute class which gives me the ExceptionContext instance. I want to get the parameter values from the request for exceptions logging purposes. I tried to read the body content, but it is empty.
public override void OnException(ExceptionContext context)
{
JsonSerializer serializer = new JsonSerializer();
var content = context.HttpContext.Request;
using (var stream = new StreamReader(content.Body, Encoding.UTF8))
using(var jsonTextReader = new JsonTextReader(stream))
{
var bodyContent = serializer.Deserialize(jsonTextReader);
if (bodyContent != null)
exceptionData.Parameters = bodyContent.ToString();
}
}
Can anyone give me directions?