3

I'm looking at an example project which has an API controller:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace Example.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

Now when the API endpoint is invoked the returned data is serialized in JSON. In previous .net projects I've seen a base class responsible for seralizing the objects which gets called in the api controller itself but in this instance it seems somewhat hidden. Can anyone point me in the direction of documentation that outlines how this works / where I can look in the code itself? Because altough it's very cool it just does this I'd like to see how and where to serialization is happening!

4
  • It's magic =) Commented May 27, 2021 at 17:23
  • No I will not have it Commented May 27, 2021 at 17:33
  • LOL it's the same link as the answer points to but ok. Have a nice day. Commented May 27, 2021 at 17:41
  • Didn't see the link at the time. Sorry. Commented Jun 22, 2021 at 11:49

1 Answer 1

4

Asp.Net Core handles serialization by "output formatters" which will execute after the action returns. You can have multiple output formatters in your application (e.g., one that serializes to JSON and one that serializes to XML, ... etc.). And after the action executes, based on the Accept HTTP header's value, asp.net core will use the appropriate output formatter to serialize the response.

Output formatters are explained in detail here.

But if you're interested in reading the parts of the code responsible, maybe it's worth checking the implementations of IActionResultExecutor like ObjectResultExecutor. The action result executors are the components that asp.net core runs after the action method returns, and it'll select a formatter, perform the serialization and write to the response stream.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation! This makes a lot of sense now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.