I'm trying to publish my ASP.NET Core 7 Web API to Azure but whenever I try to do so I get this error message:
Publish has encountered an error.
Be sure that the Startup.cs for your application is calling AddSwaggerGen from within ConfigureServices in order to generate swagger file. Visit https://go.microsoft.com/fwlink/?LinkId=2131205&CLCID=0x409 for more information.A diagnostic log has been written to the following location: "C:\Users\cvale\AppData\Local\Temp\tmp4709.tmp"
.NET 7 (and 8) don't have Startup.cs
and do everything in Program.cs
. So I have AddSwaggerGen()
in my Program.cs
, yet I'm still getting the error when trying to publish.
Has anyone else run into this problem and been able to fix it? Thanks in advance!
I tried placing builder.Services.AddSwaggerGen()
in Program.cs
When I tried deploying .NET 7 WEB API to Azure App Service, even I faced the issue. I have added the below code to my program.cs file.
app.UseSwagger();
app.UseSwaggerUI(x =>
{
x.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
if(app.Environment.IsDevelopment())
x.RoutePrefix = "swagger"; // For localhost
else
x.RoutePrefix = string.Empty;
// For azure
}
);
Then, I am able to deploy the App successfully.
My complete Program.cs
file
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseSwagger();
app.UseSwaggerUI(x =>
{
x.SwaggerEndpoint("/swagger/v1/swagger.json", "Web API V1");
if(app.Environment.IsDevelopment())
x.RoutePrefix = "swagger";
else
x.RoutePrefix = string.Empty;
}
);
app.UseSwagger();
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Local output:
I deployed my Web API using the Visual Studio Publish option.
Successful Deployment
Azure Web App Output :