# 4.5.1 Configurar Swagger

Si aún no instalas el paquete de Swagger es hora de instalarlo.&#x20;

1. Instalar el paquete de Swagger **Swashbuckle.AspNetCore**
2. En el archivo **Startup.cs** en el método **ConfigureServices** agregamos el generador de Swagger, los parámetros son el número de versión y un título.&#x20;

{% code title="Startup.cs" %}

```csharp
using Microsoft.OpenApi.Models;
public void ConfigureServices(IServiceCollection services)
{
   services.AddControllers()
      .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
   services.AddDbContext<CaducaContext>
      (options => options.
          UseMySql(
            Configuration.GetConnectionString("DefaultConnection")));
   //Se agrega en generador de Swagger
   services.AddSwaggerGen(c =>
   {
       c.SwaggerDoc("v1", new OpenApiInfo 
              { Title = "Api Caduca REST", Version = "v1" });
   });
}
```

{% endcode %}

3\. En el método **Configure** del mismo archivo **Startup.cs** indicamos que utilizaremos Swagger con la ruta del json de configuración.

{% code title="Startup.cs" %}

```csharp
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    //Habilitar swagger
    app.UseSwagger();

    //indica la ruta para generar la configuración de swagger
    app.UseSwaggerUI(c =>
    {
         c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api Caduca REST");
    });
    app.UseMvc();
}
```

{% endcode %}

Si quieres que la ayuda corra directamente en [http://localhost:5000](http://localhost:5000/swagger/index.html) en lugar de que swagger corra en la url <http://localhost:5000/swagger/index.html> agrega la instrucción **c.RoutePrefix = string.Empty;**

```csharp
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())

    //indica la ruta para generar la configuración de swagger
    app.UseSwaggerUI(c =>
    {
         c.SwaggerEndpoint("/swagger/v1/swagger.json", "Api Caduca REST");
         c.RoutePrefix = string.Empty;
    });
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://abi.gitbook.io/net-core/4.-creando-tu-primer-servicio/4.5-documentar-y-probar-tus-servicios-con-swagger/4.5.1-configurar-swagger.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
