I'm able to achieve same behavior easily in .NET Framework but coming to .NET Core I don't find needed methods to achieve same
Snippet: below response has only Getter no setter and CreateErrorResponse is not supported
var error = new HttpError
{
{
"data", reqdInfo
}
};
context.Response = context.Request.CreateErrorResponse(HttpStatusCode.BadRequest, error);
I want to include above error info in the response before i give back control to controller
Edit: In framework
ASP.NET was heavily reworked since the pre-.NET Core version. Something potentially similar can be achieved with Results
class (available since ASP.NET Core 6). For example:
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.Equals("/api/test/error", StringComparison.OrdinalIgnoreCase))
{
await Results.Problem(new ProblemDetails
{
Extensions = { { "data", new { Value = 1 } } }
})
.ExecuteAsync(context);
}
else
{
await next();
}
});
Which produces the following result for /api/test/error
:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.6.1",
"title": "An error occurred while processing your request.",
"status": 500,
"data": {
"value": 1
}
}
Potentially useful articles: