DEV Community

Cover image for Swagger Export from .NETCore
Ankur Charan
Ankur Charan

Posted on

1

Swagger Export from .NETCore

If you have an ASP.NET Web API project. Chances are you are using Swashbuckle.AspNet.Core to generate swagger documentation for the application.

Now if you need some way to export the swagger api documentation during your build for CI/CD or any other things, this is for you.

Pre-requisite

I am assuming you already have a service from which you can access the swagger JSON/UI.

Generating Swagger on Build

set up dotnet-tools

  • Run dotnet new tool-manifest at root of your project.
  • it will create a .config directory with dotnet-tools.json in it. this file will have references to the packages that you've installed.
  • install swashbuckle cli. run dotnet tool install --local Swashbuckle.AspNetCore.Cli

add post build step

  • in the project csproj file, add a new post build step.
  • add step
<Target Name="SwaggerGenerate" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet swagger tofile --output /path/to/swagger.json .\$(OutputPath)$(AssemblyName).dll v1" />
</Target>
Enter fullscreen mode Exit fullscreen mode
  • OutputPath and AssemblyName are the project variable, and they have the path to where the build is and dll is generated.
  • If you have multiple version of the APIs, you can have v1 or v2 in the second command.

  • Build your csproj and your openapi spec (swagger json) is created during the build.

PostScript

Feel free to reach out to me if you need any help.

linkedin.com/in/ankurcharan
instagram.com/ankurcharan
dev.to/ankurcharan
twitter.com/ankurcharan
ankurcharan.hashnode.dev

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay