How to fix "No webpage was found for the web address: http://localhost:5000/swagger/v1/swagger.json" error?

Mara McConnell 25 Reputation points
2024-12-09T14:54:11.3333333+00:00

I am working on the "Improve the developer experience of an API with Swagger documentation" module.

When I run the code and navigate to [http://localhost:5000/api/priceframe/6/17], the browser looks as expected. However, after adding the Swashbuckle.AspNetCore package, making the specified code changes and trying to navigate to [http://localhost:5000/swagger/v1/swagger.json], I get the following error on my browser:

This localhost page can’t be found

No webpage was found for the web address: http://localhost:5000/swagger/v1/swagger.json

HTTP ERROR 404

Why isn't this working?

Here is what the code in Startup.cs file looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;

namespace PrintFramerAPI
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                    c.RoutePrefix = string.Empty;
                });

                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

This question is related to the following Learning Module

ASP.NET Core Training
ASP.NET Core Training
ASP.NET Core: A set of technologies in the .NET Framework for building web applications and XML web services.Training: Instruction to develop new skills.
46 questions
{count} vote

Accepted answer
  1. Pradeep M 6,655 Reputation points Microsoft External Staff
    2024-12-10T10:31:56.18+00:00

    Hi Mara McConnell

    Thank you for reaching out to Microsoft Q & A forum.  

    This issue is likely caused by the absence of a launchSettings.json file. This file is essential for defining the application's URL and environment settings during local development. Without it, the application might not run at the expected URL or be properly configured to serve Swagger. 

    To resolve this issue, create the missing launchSettings.json file. This file configures the application's URL, specifies whether HTTPS should be used, and sets environment variables. 

    1.Create the launchSettings.json File 

    In your project directory, navigate to the Properties folder. 

    If the Properties folder doesn’t exist, create it. 

    Inside the folder, create a file named launchSettings.json. 

    2.Add Configuration to the File 

    Paste the following content into launchSettings.json: 

    {
        "profiles": {
          "PrintFramerAPI": {
            "commandName": "Project",
            "dotnetRunMessages": true,
            "launchBrowser": true,
            "applicationUrl": "
            "environmentVariables": {
              "ASPNETCORE_ENVIRONMENT": "Development"
            }
          }
        }
      }
    

    "applicationUrl": "http://localhost:5000",

    3.Run the Application 

    Save your changes (Ctrl+S),

    Restart the application using the following command: 

    dotnet run
    
    

    Please feel free to contact us if you have any additional questions.    

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.               

     

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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