Keywords: Swagger | ASP.NET Core | Swashbuckle | API Documentation | Error Diagnosis
Abstract: This article addresses the common issue of Swagger UI loading failures in ASP.NET Core projects, specifically the 'Fetch error undefined ./swagger/v1/swagger.json' error. It provides systematic diagnostic methods and solutions by analyzing core problems such as Swashbuckle configuration, routing setup, and API definition conflicts. Utilizing practical techniques like developer exception pages, network monitoring, and configuration adjustments, the guide helps developers quickly identify and fix common obstacles in Swagger integration. Based on real-world cases, it thoroughly explains error root causes and repair steps, suitable for web application development in environments like IIS Express.
Problem Background and Error Phenomenon
When integrating Swagger UI into ASP.NET Core web applications, developers often encounter the Fetch error undefined ./swagger/v1/swagger.json error. This indicates that Swagger UI fails to load the API definition file, typically occurring when the application is hosted on IIS Express or similar environments. Initial configurations might display the Swagger page correctly, but the API list remains empty, with this error visible in the console or network requests.
Core Diagnostic Methods
To effectively diagnose this issue, start by enabling detailed error information. Add app.UseDeveloperExceptionPage(); to the Startup.Configure method. This allows viewing specific exceptions when navigating to the swagger/v1/swagger.json endpoint, instead of the vague 'undefined' error. For instance, it might reveal root causes like path or method conflicts.
Additionally, use the browser's developer tools network tab to monitor the swagger.json request response. This directly shows error details returned by the server, such as HTTP status codes and message bodies, aiding in identifying configuration or code issues.
Common Issues and Solutions
API Operation Conflicts
A frequent cause is multiple API operations sharing the same path and method, leading to Swagger generation failure. This easily happens when operations in dependent libraries or controllers are not properly distinguished. The solution is to modify the Swagger generation configuration in the ConfigureServices method:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});This code resolves conflicts using the ResolveConflictingActions method, selecting the first matching operation as the definition. Note that this might cause some APIs to be ignored, so it's advisable to fix the source operation definitions when possible.
Endpoint Path Configuration
In environments like IIS Express or virtual directories, the default Swagger JSON endpoint path might be incorrect. Adjust the endpoint setting in UseSwaggerUI:
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./v1/swagger.json", "My API V1");
});Changing the path from ./swagger/v1/swagger.json to ./v1/swagger.json adapts to the routing rules of specific hosting environments. This change is based on practical testing and may vary depending on the application's base path or IIS configuration.
Undecorated Public Methods
Swagger relies on REST attributes (e.g., [HttpGet], [HttpPost]) to identify API operations. If there are undecorated public methods in controllers, Swagger might not handle them correctly, causing generation errors. Check all public methods to ensure they are decorated with appropriate HTTP attributes, or change non-API methods to private.
Complete Configuration Example and Best Practices
Below is a corrected Startup class example that integrates the above solutions:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "API WSVAP (WebSmartView)", Version = "v1" });
c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("./v1/swagger.json", "My API V1");
c.RoutePrefix = string.Empty;
});
app.UseMvc();
}
}Best practices include: always enabling the exception page in development environments; regularly verifying the accessibility of the Swagger JSON endpoint; using version control to avoid operation conflicts; and standardizing API decoration in team projects.
Summary and Extended Recommendations
Swagger integration errors often stem from configuration mismatches or API definition issues. Through systematic diagnosis and targeted adjustments, functionality can be quickly restored. For complex projects, consider using Swagger filters to customize document generation or integrating API versioning to enhance maintainability. Refer to official documentation and community resources, such as the Microsoft ASP.NET Core Swagger guide, for the latest best practices.