Files
ApiVersioningDemo/ApiVersioningDemo.Api/Controllers/WeatherForecastController.cs

28 lines
1.0 KiB
C#

// ApiVersioningDemo.Api/Controllers/WeatherForecastController.cs
using Asp.Versioning;
using Microsoft.AspNetCore.Mvc;
namespace ApiVersioningDemo.Api.Controllers;
[ApiController]
[ApiVersion("1.0")] // 1. Le decimos explícitamente que este es el controlador de la v1
[Route("api/v{version:apiVersion}/[controller]")] // 2. Modificamos la ruta para obligar a usar "api/v1/..."
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = [
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching", "SuperScorching"
];
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return 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();
}
}