You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Swashbuckle.AspNetCore.Cli v6.4.0生成swagger.json遇MSB3073错误

PostBuild生成Swagger JSON失败及解决过程

我在.csproj文件中添加了以下配置:

<Target Name="SwaggerPostBuildTarget" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore"></Exec>
    <Exec Command="dotnet tool install Swashbuckle.AspNetCore.Cli --version 6.4.0"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v1/swagger.json $(OutputPath)$(AssemblyName).dll v1"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v2/swagger.json $(OutputPath)$(AssemblyName).dll v2"></Exec>
    <Exec Command="dotnet swagger tofile --output swagger/v3/swagger.json $(OutputPath)$(AssemblyName).dll v3"></Exec>
</Target>

运行项目时出现错误:

MSB3073 - The command "dotnet swagger tofile --output /swagger/v1/swagger.json bin\Debug\net6.0\xxx.dll v1" exited with code -532462766

我已严格按照官方文档操作,但问题仍未解决。


更新1

经多次尝试发现,执行PostBuild命令时环境变量未设置,而我的代码需要读取环境变量,这是错误根源。同时发现项目监听端口与launchsettings.json中设置不符,但不清楚端口来源。

更新2

我参考了相关技术社区问题和对应项目Issue,但仍无法无错构建项目。

更新3

我已成功生成swagger.json文件,当前.csproj配置见截图,Program.cs代码为:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Custom configurations

var startup = new Startup(builder.Configuration);

startup.ConfigureServices(builder.Services);

var app = builder.Build();

startup.Configure(app, app.Environment, app.Services.GetRequiredService<IApiVersionDescriptionProvider>());

app.Run();

内容的提问来源于stack exchange,提问作者Emanuele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 23:36:38