Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to respond via an JsonResult from Asp.Net Core middleware?

I would like to respond via a JsonResult from a piece of Asp.Net Core middleware but it's not obvious how to accomplish that. I have googled around alot but with little success. I can respond via a JsonResult from a global IActionFilter by setting the ActionExecutedContext.Result to the JsonResult and that's cool. But in this case I want to effectively return a JsonResult from my middleware. How can that be accomplished?

I framed the question with regard to the JsonResult IActionResult but ideally the solution would work for using any IActionResult to write the response from the middleware.

like image 460
RonC Avatar asked Mar 29 '17 22:03

RonC


People also ask

How do I return JSON in Web API NET Core?

To return data in a specific format from a controller that inherits from the Controller base class, use the built-in helper method Json to return JSON and Content for plain text. Your action method should return either the specific result type (for instance, JsonResult ) or IActionResult .

What does JsonResult return?

JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).

How does ASP.NET Core middleware work?

Middleware in ASP.NET Core controls how our application responds to HTTP requests. It can also control how our application looks when there is an error, and it is a key piece in how we authenticate and authorize a user to perform specific actions.


2 Answers

Middleware is a really low-level component of ASP.NET Core. Writing out JSON (efficiently) is implemented in the MVC repository. Specifically, in the JSON formatters component.

It basically boils down to writing JSON on the response stream. In its simplest form, it can be implemented in middleware like this:

using Microsoft.AspNetCore.Http; using Newtonsoft.Json;  // ...  public async Task Invoke(HttpContext context) {     var result = new SomeResultObject();     var json = JsonConvert.SerializeObject(result);     await context.Response.WriteAsync(json); } 
like image 105
Henk Mollema Avatar answered Oct 15 '22 07:10

Henk Mollema


For others that may be interested in how to return the output of a JsonResult 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 a JsonResult and the approach is close to being able to enable middleware to return the output from any IActionResult. To handle that more generic case the code for creating the ActionDescriptor would need improved. But taking it to this point was sufficient for my needs of returning the output of a JsonResult.

like image 44
RonC Avatar answered Oct 15 '22 06:10

RonC