.NET APIs Part 6 – Swagger

This is part 6 in a series about building APIs in .NET using C#. The previous (part 5) entry is here, and the series starts here.

As you know, an API sits between a client and the back end. It is imperative for the client programmer to know not only what an API does, but what the URL is, what verbs it supports and what parameters are available.

Fortunately, there is an open standard and free server: Open API and Swashbuckle. You install these once for each project and then you can just use them, as we’ll see.

This post will show you how to install them, using Visual Studio. You’ll obtain the bits you need using NuGet. Start by installing Swashbuckle.AspNetCore.

Go to your project’s properties and choose Application and Console Application. Then choose Output under Build and scroll down to where you can check “Generate a file containing API documentation”

Open Program.cs and add the Swagger generator to the services collection:

builder.Services.AddSwaggerGen(
 x =>
 {
     x.SwaggerDoc(
         "v1",
         new OpenApiInfo
         {
             Title = $"{Assembly.GetExecutingAssembly().GetName().Name}",
             Version = "Version 1",
             Description = "Create documentation for myApp",
             Contact = new OpenApiContact
             {
                 Name = "Jesse Liberty",
                 Email = "yourName@gmail.com",
                 Url = new Uri("https://jesseliberty.com")
             }
         });
             var xmlFilename = System.IO.Path.Combine(System.AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml");
             c.IncludeXmlComments(xmlFilename);

});

That's it, you are now ready to create Swagger documentation. We'll review how to do so in the next installment.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in API, C# and tagged . Bookmark the permalink.

4 Responses to .NET APIs Part 6 – Swagger

  1. list.ly says:

    Fascinating and perceptive analysis of the subject matter.
    Your review was extensive and well-referenced, giving subscribers with a comprehensive understanding
    of the essential concerns at fingertips.

    I might be excited to converse supplementary on this
    content. If you approve, I would graciously suggest you to join me on the SBOBET
    site, in which we should extend our conversation in a more responsive space.

    My web site: online casino cybersecurity (list.ly)

  2. The world of rigorous gaming has undergone a remarkable change in recent years, with the rise of esports as a
    global trend . Amidst this rapidly evolving landscape,
    one name has emerged as a innovator – Spade Gaming.

    Spade Gaming is a dominance to be reckoned with, a gaming
    corporation that has carved out a unique
    expertise for itself by blending cutting-edge invention ,
    strategic foresight , and a unwavering commitment to prowess.

    Established with the goal of rethinking the boundaries of
    cutthroat gaming, Spade Gaming has quickly become a exemplar of ingenuity , driving the
    market forward with its unconventional approach and
    unyielding dedication.

    At the center of Spade Gaming’s triumph lies its steadfast drive on player development and unit building.

    The establishment has cultivated an community that encourages and reinforces its performers , providing them with the supplies , instruction , and
    assistance they need to reach new heights .

    But Spade Gaming’s impact extends far beyond the reach
    of the parameters of the game inherently . The corporation has also solidified itself as
    a pacesetter in the discipline of information creation, leveraging its
    vast repository of talented individuals to create
    spellbinding and spellbinding coverage that resonates supporters throughout the planet
    .

    In addition , Spade Gaming’s loyalty to public stewardship and community
    outreach differentiates it one-of-a-kind from its competitors .
    The conglomerate has utilized its stage to raise awareness
    for consequential crusades , deploying its significance and
    prominence to foster a impactful effect in the sphere of esports and stretching wider .

    As the esports sector presses forward to
    advance , Spade Gaming towers as a shining prototype of that which can be realized when planning , originality , and
    a determined pursuit of perfection fuse
    .

    In the epoch to follow , as the world of challenging gaming persists to enthrall
    spectators and reinvent the way we engage with amusement , Spade Gaming will indisputably endure
    at the forefront , pioneering the drive and building a pioneering age in the
    constantly changing realm of esports.

    my web page: online casino player feedback

  3. Andrii says:

    I heard that Swashbuckle package don’t support NET8

  4. Pingback: Dew Drop – February 6, 2024 (#4122) – Morning Dew by Alvin Ashcraft

Leave a Reply

Your email address will not be published. Required fields are marked *