For others who might be wondering how to return JsonResult output from middleware, this is what I came up with:
public async Task Invoke(HttpContext context, IHostingEnvironment env) { JsonResult result = new JsonResult(new { msg = "Some example message." }); RouteData routeData = context.GetRouteData(); ActionDescriptor actionDescriptor = new ActionDescriptor(); ActionContext actionContext = new ActionContext(context, routeData, actionDescriptor); await result.ExecuteResultAsync(actionContext); }
This approach allows a piece of middleware to return output from JsonResult , and the approach is close to being able to include middleware to return output from any IActionResult. To handle this more general case, the code for creating an ActionDescriptor will need to be improved. But doing this up to this point was enough for my needs to return JsonResult output.
source share