2026-03-07 20:27:24 -03:00
|
|
|
// 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"
|
2026-03-07 20:41:26 -03:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
[HttpGet(Name = "GetWeatherForecastV2")]
|
|
|
|
|
// CAMBIO 1: Cambiamos IActionResult por ActionResult<TipoConcreto>
|
|
|
|
|
public ActionResult<WeatherForecastResponseV2> Get()
|
2026-03-07 20:27:24 -03:00
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
|
2026-03-07 20:41:26 -03:00
|
|
|
// CAMBIO 2: Devolvemos el record tipado en lugar del anónimo
|
|
|
|
|
var response = new WeatherForecastResponseV2("Buenos Aires", forecast);
|
|
|
|
|
|
|
|
|
|
return Ok(response);
|
2026-03-07 20:27:24 -03:00
|
|
|
}
|
2026-03-07 20:41:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Definición fuerte de la respuesta para que Swagger la entienda
|
|
|
|
|
public record WeatherForecastResponseV2(string Ciudad, IEnumerable<WeatherForecast> Pronosticos);
|