diff --git a/ApiVersioningDemo.Api/Controllers/WeatherForecastV2Controller.cs b/ApiVersioningDemo.Api/Controllers/WeatherForecastV2Controller.cs new file mode 100644 index 0000000..9b2273e --- /dev/null +++ b/ApiVersioningDemo.Api/Controllers/WeatherForecastV2Controller.cs @@ -0,0 +1,31 @@ +// ApiVersioningDemo.Api/Controllers/WeatherForecastV2Controller.cs +using Asp.Versioning; +using Microsoft.AspNetCore.Mvc; + +namespace ApiVersioningDemo.Api.Controllers; + +[ApiController] +[ApiVersion("2.0")] // 1. ¡Esta es la clave! Le decimos que es la versión 2. +[Route("api/v{version:apiVersion}/WeatherForecast")] // 2. Forzamos la ruta para que responda a la v2 +public class WeatherForecastV2Controller : ControllerBase +{ + private static readonly string[] Summaries = [ + "Helado", "Frío", "Fresco", "Templado", "Cálido", "Caluroso", "Sofocante", "SuperScorching" + ]; [HttpGet(Name = "GetWeatherForecastV2")] + public IActionResult Get() // Cambiamos a IActionResult para devolver un objeto anónimo + { + var forecast = Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }).ToArray(); + + // 3. EL BREAKING CHANGE: Ahora devolvemos un objeto con la "Ciudad" y la lista adentro + return Ok(new + { + Ciudad = "Buenos Aires", + Pronosticos = forecast + }); + } +} \ No newline at end of file