1. First, check if you have uploaded all necessary files of your API site to the server.
We recommend using
Web Deploy to publish your application, this will package your app to production files and upload them to the destination server.
2. Second, if the publish progress went smoothly, but you still receive error 404.
Please check the Startup.cs(.net core version lower than 6.0) or Program.cs(.net core version 6.0), make sure you have registered the Controller middleware properly and configured correct route for the API endpoint
Startup.cs
public class Startup
{
...
...
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
...
...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
...
...
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Program.cs
...
builder.Services.AddControllers();
...
...
app.MapControllers();
app.Run();
API endpoint
// these settings can be customized
[Route("api/[controller]")]
[ApiController]
[HttpGet]
3. Thrid, all API endpoints work properly, but the Swagger docs(/swagger/index.html) link returns error 404, it works on your local but on server does not.
The OpenAPI support will be placed in development code snippets default, it only takes effect in the Development environment, while the server default environment is Production.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
To make the Swagger docs work in a Production server, you can move the above code snippets outside of the if judgment. Like this:
if (app.Environment.IsDevelopment())
{
}
app.UseSwagger();
app.UseSwaggerUI();
4. After you finish the above, please republish your API app to the server through Web Deploy and retry. If you still get an error when browsing the production site, you can enable the logging system of ASP.NET Core to diagnose.
5. Please feel free to contact our support if you need assistance.
Article ID: 2209, Created: February 17, 2022 at 9:24 PM, Modified: June 5 at 2:57 AM