Swagger Json in Postman does not put the API URL

Asked

Viewed 281 times

1

I am using the Swagger library on . NET CORE in a Web API to generate the documentation, below is my startup.Cs:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        services.AddCors();
        services.AddTransient<Context>();

        // Register the Swagger generator, defining 1 or more Swagger documents
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Basic API",
                Description = "A simple example ASP.NET Core Web API",

            });

            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });


    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();

        // Enable middleware to serve generated Swagger as a JSON endpoint.
        app.UseSwagger();

        // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
        // specifying the Swagger JSON endpoint.
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "Example Api v1");
            c.RoutePrefix = "docs";
        });
        app.UseMvc();
    }
}

When I get his JSON for url https://localhost:44397/swagger/v1/swagger.json, it generates me the pure JSON and I can import in POSTMAN, only it is without the base URL in the requests, as print below.

Can anyone help me to pop up the base URL when I import to POSTMAN? Something missing from Startup?

inserir a descrição da imagem aqui

  • When you use SWAGGER it usually gives you the option to make calls via browser, so you don’t need POSTMAN. But as each case is a case, if you want to access your API by Postman will access by the link: localhost://XXXX/api/controller/methodName/params (if you act).

1 answer

0

You can use Idocumentfilter to make this change:

  /// <summary>
/// Filtro Swagger
/// </summary>
public class CustomDocumentFilter : IDocumentFilter
{
    public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
    {
        swaggerDoc.BasePath = "localhost:5001";
        swaggerDoc.Schemes = new List<string> { "https" };
    }
}

And inject into Startup:

      services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new Info
            {
                Version = "v1",
                Title = "Basic API",
                Description = "A simple example ASP.NET Core Web API", 

            });
            c.DocumentFilter<CustomDocumentFilter>();

            var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
            var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
            c.IncludeXmlComments(xmlPath);
        });

inserir a descrição da imagem aqui

But it’s just an example, if you really need to do this, do it dynamically by environment type.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.