API issue on the server

Scott Huang 3,491 Reputation points
2026-03-11T06:10:53.63+00:00

Hi,

No similar issue when calling the project. Why does it appear when going to "https://localhost:7060/swagger/index.html"?

User's image

Developer technologies | ASP.NET Core | ASP.NET API

Answer accepted by question author
Damien Pham (WICLOUD CORPORATION) 2,300 Reputation points Microsoft External Staff Moderator
2026-03-12T10:24:25.5666667+00:00

Hello @Peter_1985 ,

Thank you for the snippet. From the screenshot, you have

if (app.Environment.IsProduction())  
{      
app.UsePathBase("/webapp1"); // Must match your IIS alias  
}

It is configured in Production environment, but the swaggerUI is configured in Development environment. This is likely why you only get the error when go to https://localhost:7060/swagger/index.html

To fix this, you can configure the environment to Production for both.

Hope you can resolve your issue. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide provide feedback.      

Thank you.   

Was this answer helpful?


3 additional answers

Sort by: Oldest
  1. AI answer

    2026-03-11T06:11:20.03+00:00

    The screenshot shows that Swagger UI loads, but it can’t fetch the OpenAPI document at /swagger/v1/swagger.json (NetworkError). This usually means the API is running and reachable, but the Swagger endpoint itself is misconfigured or not available at that path.

    Typical causes in ASP.NET Core Web API:

    1. Swagger middleware not mapped for the environment
      • In Program.cs, Swagger is often only enabled in Development:
         if (app.Environment.IsDevelopment())
         {
             app.UseSwagger();
             app.UseSwaggerUI();
         }
      
      If the app is running under a different environment (e.g., Production), /swagger/v1/swagger.json won’t exist, and Swagger UI will show a network error.
      • Fix: Either run the app with ASPNETCORE_ENVIRONMENT=Development or move app.UseSwagger(); app.UseSwaggerUI(); outside the IsDevelopment block if Swagger should always be available.
    2. Wrong route or document name
      • If SwaggerGen is configured with a different document name or route, the UI might still be pointing to /swagger/v1/swagger.json while the actual JSON is at another URL.
      • Fix: Ensure the document name in AddSwaggerGen matches the one used in UseSwaggerUI, for example:
         builder.Services.AddSwaggerGen(c =>
         {
             c.SwaggerDoc("v1", new() { Title = "WebApp1", Version = "v1" });
         });
         
         app.UseSwagger();
         app.UseSwaggerUI(c =>
         {
             c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApp1 v1");
         });
      
    3. HTTPS/port mismatch
      • If the app is actually listening on a different port or scheme than the one Swagger UI is using, the fetch will fail.
      • Fix: Confirm the app is running on https://localhost:7060 and that no reverse proxy or different port is used. Adjust launch profile or Swagger endpoint accordingly.
    4. Middleware order or startup failure
      • If there’s an exception during startup before UseSwagger runs, the JSON endpoint won’t be available even though the browser still shows a cached or partial UI.
      • Fix: Check application logs/console output for startup errors and ensure app.UseSwagger() is called before app.UseAuthorization()/app.MapControllers().

    Because direct calls to the API work, focus on verifying that the Swagger JSON endpoint exists and matches the URL configured in UseSwaggerUI, and that Swagger middleware is enabled for the environment where the app is running.


    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

  2. Bruce (SqlWork.com) 85,416 Reputation points
    2026-03-13T02:39:44.8166667+00:00

    You don’t specify your .net version. Your code looks like .net 8, but won’t work with later versions. The error means the json url created by swagger gen does not match the url swagger ui is expecting.

    Was this answer helpful?


  3. Bruce (SqlWork.com) 85,416 Reputation points
    2026-03-13T03:25:56.8833333+00:00

    You don’t specify your .net version. Your code looks like .net 8, but won’t work with later versions. The error means the json url created by swagger gen does not match the url swagger ui is expecting. Check the overloads to specify the path explicitly.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.