feat: implementar versionado de API por URL v1. Closes #3
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Asp.Versioning.Http" Version="8.1.1" />
|
||||
<PackageReference Include="Asp.Versioning.Mvc.ApiExplorer" Version="8.1.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
// ApiVersioningDemo.Api/Controllers/WeatherForecastController.cs
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace ApiVersioningDemo.Api.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
[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 =
|
||||
|
||||
@@ -1,7 +1,28 @@
|
||||
// ApiVersioningDemo.api/Program.cs
|
||||
using Asp.Versioning;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// CONFIGURACIÓN DE VERSIONADO
|
||||
builder.Services.AddApiVersioning(options =>
|
||||
{
|
||||
// Si el cliente no especifica versión, usaremos la 1.0 por defecto
|
||||
options.DefaultApiVersion = new ApiVersion(1, 0);
|
||||
options.AssumeDefaultVersionWhenUnspecified = true;
|
||||
|
||||
// Esto añade una cabecera HTTP en las respuestas diciendo qué versiones existen (ej: api-supported-versions: 1.0, 2.0)
|
||||
options.ReportApiVersions = true;
|
||||
})
|
||||
.AddMvc() // Integra el versionado con los Controladores
|
||||
.AddApiExplorer(options =>
|
||||
{
|
||||
// Configura el formato para Swagger (ej: "v1", "v2")
|
||||
options.GroupNameFormat = "'v'VVV";
|
||||
options.SubstituteApiVersionInUrl = true;
|
||||
});
|
||||
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
Reference in New Issue
Block a user