Skip to content


Rico Suter edited this page Nov 13, 2019 · 3 revisions

Package: NSwag.MSBuild

Important for .NET Core: Assembly loading#net-core

After installing the NSwag.MSBuild NuGet package in your project, you can use the variable $(NSwagExe) in your .csproj file to run the NSwag command line tool in an MSBuild target. This way the tools can easily be updated via NuGet. The /controller parameter can be omitted to generate a Swagger specification for all controllers.

<Target Name="NSwag" AfterTargets="Build">
    <Exec Command="$(NSwagExe) aspnetcore2openapi /assembly:$(TargetDir)MyWebAssembly.dll /output:swagger.json" />
    <Exec Command="$(NSwagExe) openapi2tsclient /input:swagger.json /output:Scripts/MyController.ts" />

For better testability and stable output (defaults may change), it is recommended to create an NSwag Configuration Document (e.g. with NSwagStudio) and use:

<Target Name="NSwag" AfterTargets="Build">
    <Exec Command="$(NSwagExe) run nswag.json /variables:Configuration=$(Configuration)" />

Recommended for .NET Core 2.0/2.1:

<Target Name="NSwag" AfterTargets="Build">
  <Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />
  <Exec Command="$(NSwagExe_Core20) run nswag.json /variables:Configuration=$(Configuration)" />
  <RemoveDir Directories="$(OutDir)References" />

Recommended for .NET Core 2.2:

<Target Name="NSwag" AfterTargets="Build">
  <Copy SourceFiles="@(ReferencePath)" DestinationFolder="$(OutDir)References" />
  <Exec Command="$(NSwagExe_Core22) run nswag.json /variables:Configuration=$(Configuration),OutDir=$(OutDir)" />
  <RemoveDir Directories="$(OutDir)References" />

For more information about the missing DLLs in .NET Core, see Assembly loading#net-core.

More information on nswag run.


If you need to run the NSwag command line in x86 mode, use the $(NSwagExe_x86) placeholder. For .NET Core projects, one of the placeholders $(NSwagExe_Core##) (e.g. $(NSwagExe_Core20) for .NET Core 2.0) should be used instead of $(NSwagExe).

Available properties:

  • NSwagExe
  • NSwagExe_x86
  • NSwagExe_Core10
  • NSwagExe_Core11
  • NSwagExe_Core20
  • NSwagExe_Core21
  • NSwagExe_Core22
  • NSwagExe_Core30
  • NSwagDir
  • NSwagDir_Core10
  • NSwagDir_Core11
  • NSwagDir_Core20
  • NSwagDir_Core21
  • NSwagDir_Core22
  • NSwagDir_Core30


See Command Line for more information.

Also see ServiceProjectReference for the new feature.

You can’t perform that action at this time.