Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macOS - no Swagger on Tutorial: Create a web API #21647

Closed
downshiftdata opened this issue Mar 1, 2021 · 28 comments · Fixed by #21661
Closed

macOS - no Swagger on Tutorial: Create a web API #21647

downshiftdata opened this issue Mar 1, 2021 · 28 comments · Fixed by #21661
Assignees
Labels
doc-enhancement seQUESTered Identifies that an issue has been imported into Quest. Source - Docs.ms Docs Customer feedback via GitHub Issue

Comments

@downshiftdata
Copy link

downshiftdata commented Mar 1, 2021

Since this is a "Hello World" kind of situation, more help when things go wrong would be appreciated. I followed the instructions and the /Swagger URL wouldn't load. I'm sure I'm missing some prerequisite, but don't know what yet.


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.


Associated WorkItem - 97033

@dotnet-bot dotnet-bot added ⌚ Not Triaged Source - Docs.ms Docs Customer feedback via GitHub Issue labels Mar 1, 2021
@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Mar 2, 2021

Most likely you don't have .NET 5 installed or unchecked Enable OpenAPI support

image

@philiptkd

This comment has been minimized.

@Rick-Anderson

This comment has been minimized.

@Rick-Anderson Rick-Anderson reopened this Mar 3, 2021
@Rick-Anderson Rick-Anderson changed the title Followed instructions but no Swagger no Swagger on Tutorial: Create a web API with ASP.NET Core Mar 3, 2021
@Rick-Anderson Rick-Anderson self-assigned this Mar 3, 2021
@Rick-Anderson Rick-Anderson reopened this Mar 3, 2021
@rajalaxmijena

This comment has been minimized.

@drock702
Copy link

drock702 commented Jun 24, 2021

I'm trying this tutorial on my MacBook, and I see the WeatherForecast page, but the swagger page is not loading.
The page never loads, it is just blank.
I have .NET 5 installed and I checked Enable OpenAPI support

@paulzolnierczyk
Copy link

I'm trying this tutorial on my MacBook, and I see the WeatherForecast page, but the swagger page is not loading.
The page never loads, it is just blank.
I have .NET 5 installed and I checked Enable OpenAPI support

I'm in the same situation as @drock702

@drock702
Copy link

I'm trying this tutorial on my MacBook, and I see the WeatherForecast page, but the swagger page is not loading.
The page never loads, it is just blank.
I have .NET 5 installed and I checked Enable OpenAPI support

I'm in the same situation as @drock702

@paulzolnierczyk - I got it to work by installing Swagger/Swashbuckle package and "Rebuild All" my project.
https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-5.0&tabs=visual-studio-mac

@Rick-Anderson
Copy link
Contributor

@wadepickett can you test this on macOS?

@Rick-Anderson Rick-Anderson changed the title no Swagger on Tutorial: Create a web API with ASP.NET Core macOS - no Swagger on Tutorial: Create a web API Jul 14, 2021
@rsoaresgouveia
Copy link

rsoaresgouveia commented Aug 19, 2021

Hello. Im currently using Visual Studio on Mac, and in tutorial page the swagger initial configuration for it to work is missing. So in "Test your project" section, the swagger configuration is missing, leaving /Startup.cs as follows:
.
.
.
`
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

        services.AddControllers();

        //Swagger configuration
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "TodoApi", 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.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1"));

        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

.
.
.

This bit of code necessary to configure swagger is shown in the same page in the section "Register the database context" (the lines commented).

I hope it helps.

@SwaraliJoshi
Copy link

SwaraliJoshi commented Aug 21, 2021

I am still not able to run the test api provided in the "https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio-code". localhost:5001(/swagger) shows just "This page isn’t workinglocalhost didn’t send any data.
ERR_EMPTY_RESPONSE"

Please help asap.
@Rick-Anderson

Note: Using VS Code

@paroqa02
Copy link

paroqa02 commented Sep 1, 2021

Hello. Im currently using Visual Studio on Mac, and in tutorial page the swagger initial configuration for it to work is missing. So in "Test your project" section, the swagger configuration is missing, leaving /Startup.cs as follows:
.
.
.
`
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{

        services.AddControllers();

        //Swagger configuration
        services.AddSwaggerGen(c =>
        {
            c.SwaggerDoc("v1", new OpenApiInfo { Title = "TodoApi", 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.UseDeveloperExceptionPage();
            app.UseSwagger();
            app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TodoApi v1"));

        }

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthorization();

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

.
.
.

This bit of code necessary to configure swagger is shown in the same page in the section "Register the database context" (the lines commented).

I hope it helps.

I try to paste the swagger config code to the startup.cs file , but getting error :(

@rsoaresgouveia
Copy link

I tried a new project to see what was happening. First go to your project root folder and run:

dotnet add package Swashbuckle.AspNetCore -v 5.6.3

This will add the Swashbuckle package into your project containing Swagger.
Than you can change your Startup.cs following:

  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Microsoft.OpenApi.Models;

  namespace <your_namespace_here>
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
          public IConfiguration Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
  
              services.AddControllers();
              services.AddSwaggerGen(c =>
              {
                  c.SwaggerDoc("v1", new OpenApiInfo { Title = "GpioService", 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.UseDeveloperExceptionPage();
                  app.UseSwagger();
                  app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "GpioService v1"));
              }
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =>
              {
                  endpoints.MapControllers();
              });
          }
      }
  }

Just change your namespace to your project namespace.
For me it's working like a charm:
Screen Shot 2021-09-01 at 18 51 57

I hope it helps.

@twakil
Copy link

twakil commented Sep 15, 2021

Make sure you have the .NET 5.0 SDK installed and that .NET 5.0 is selected as the target framework in the project properties. For those who are experiencing this problem on Windows, make sure you're using Chrome rather than IE to load the Swagger page. In IE it just shows a blank page, but in Chrome it works.

@Rick-Anderson
Copy link
Contributor

make sure you're using Chrome rather than IE to load the Swagger page. In IE it just shows a blank page, but in Chrome it works.

Edge works too. Thanks for the tip.

@paroqa02
Copy link

paroqa02 commented Sep 16, 2021 via email

@yiruli
Copy link

yiruli commented Oct 23, 2021

This video is the solution:
https://www.youtube.com/watch?v=UG1AdlmrJGQ&t=289s
Create ASP.Net Core Web API project using CLI and VS Code | ASP.Net Core Web API Tutorial

@shad-1
Copy link

shad-1 commented Oct 26, 2021

I tried a new project to see what was happening. First go to your project root folder and run:

dotnet add package Swashbuckle.AspNetCore -v 5.6.3

This will add the Swashbuckle package into your project containing Swagger. Than you can change your Startup.cs following:

  using Microsoft.AspNetCore.Builder;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.Extensions.Configuration;
  using Microsoft.Extensions.DependencyInjection;
  using Microsoft.Extensions.Hosting;
  using Microsoft.OpenApi.Models;

  namespace <your_namespace_here>
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
          public IConfiguration Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
  
              services.AddControllers();
              services.AddSwaggerGen(c =>
              {
                  c.SwaggerDoc("v1", new OpenApiInfo { Title = "GpioService", 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.UseDeveloperExceptionPage();
                  app.UseSwagger();
                  app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "GpioService v1"));
              }
  
              app.UseRouting();
  
              app.UseAuthorization();
  
              app.UseEndpoints(endpoints =>
              {
                  endpoints.MapControllers();
              });
          }
      }
  }

Just change your namespace to your project namespace. For me it's working like a charm: Screen Shot 2021-09-01 at 18 51 57

I hope it helps.

This fixed the problem for me. (Using VS for mac).
Note that there are several Swashbuckle packages you can install, and the one referenced in this reply contains all the needed child packages.

@paroqa02
Copy link

paroqa02 commented Oct 26, 2021 via email

@rohitsiddha
Copy link

Seems like a bug in macOS Visual Studio. While creating a web API project, the Enable Open API support checkbox works otherwise.
Uncheck it to add /swagger endpoint to your project. This is weird but worked.

Screenshot 2021-11-23 at 4 29 42 AM

@Rick-Anderson
Copy link
Contributor

@rohitsiddha why are you using .NET 5 and not .NET 6?

@kieron-cairns
Copy link

@rohitsiddha this happened to me too thank you

@mattsonnhalter
Copy link
Contributor

@rohitsiddha Thank you for the the fix. What an odd bug. Deselecting Enable OpenAPI support actually enables it. I am using .Net 5 as well (the current recommendation for M1 Macs).

@bushra4468
Copy link

Seems like a bug in macOS Visual Studio. While creating a web API project, the Enable Open API support checkbox works otherwise. Uncheck it to add /swagger endpoint to your project. This is weird but worked.

Screenshot 2021-11-23 at 4 29 42 AM

This worked for me! After hours of scrolling and downloading

@jhonatasmatos
Copy link

Seems like a bug in macOS Visual Studio. While creating a web API project, the Enable Open API support checkbox works otherwise. Uncheck it to add /swagger endpoint to your project. This is weird but worked.

Screenshot 2021-11-23 at 4 29 42 AM

it worked for me, thanks !!!

@wadepickett wadepickett added the reQUEST Triggers an issue to be imported into Quest label Jun 2, 2023
@github-actions github-actions bot added seQUESTered Identifies that an issue has been imported into Quest. and removed reQUEST Triggers an issue to be imported into Quest labels Jun 3, 2023
@wadepickett wadepickett moved this from 🔖 Ready to 🏗 In progress in dotnet/AspNetCore.Docs June 2023 Jun 9, 2023
@wadepickett
Copy link
Contributor

wadepickett commented Jun 21, 2023

Thanks very much to all of you who relayed your experience and identified the issue!

This appears to have been fixed and working as expected in the current version 17.6 (build 1575) of Visual Studio for Mac when targeting .NET 5. The Enable OpenAPI support setting results in Swagger working properly.

I also verified it continues to work properly in the new Visual Studio 2022 for Mac Preview with .NET 5 and I checked the latest Visual Studio 2022 17.6.4 for windows and .NET 5. Works as expected.

The prerequisites for the .NET 5 version of the tutorial point to the latest version of Visual Studio for Mac, which is what should be used. I will add some text to clarify it is the latest version that is required in the prerequisites.

When you select .NET 5 in the latest Visual Studio, you will also see the .NET 5 target setting listed as .NET 5.0 (Out of support), which it is, and receive an additional warning once the project is created. .NET 7.0.7 is the latest with Standard Term Support and .NET 6 is Long Term Support. The latest SDK's are available here: https://dotnet.microsoft.com/download/dotnet

To verify what version of Visual Studio for Mac you currently have, select the Visual Studio > About Visual Studio menu.
To update your existing Visual Studio for Mac, select the Visual Studio > Check for Updates menu.
To see what version of the .NET SDK you have installed, open a terminal and on the command line enter dotnet --info

@wadepickett
Copy link
Contributor

Fixed with #27363 .NET 7 MacOS: Tutorial: Create a web API
It was that PR completed after this issue was raised that we specified to use Visual Studio 2022 in the first step of the doc for all versions.

@github-project-automation github-project-automation bot moved this from 🏗 In progress to ✅ Done in dotnet/AspNetCore.Docs June 2023 Jun 22, 2023
@fabiogoma
Copy link

fabiogoma commented Dec 11, 2024

This issue is still happening. My current SDK version is: 9.0.101

To reproduce the issue follow the VS Code steps from: Tutorial: Create a web API with ASP.NET Core

To make it shorter, run:

Create a new project

dotnet new webapi --use-controllers -o TodoApi
cd TodoApi
dotnet add package Microsoft.EntityFrameworkCore.InMemory
code -r ../TodoApi

Generate and trust self-signed certificates

dotnet dev-certs https --trust

Start the app

dotnet run --launch-profile https

When you start the application and try to reach the swagger on your browser, you get a blank page.
Via curl the output is:
% curl -k -vvv https://localhost:7120/swagger/index.html

  • Host localhost:7120 was resolved.
  • IPv6: ::1
  • IPv4: 127.0.0.1
  • Trying [::1]:7120...
  • Connected to localhost (::1) port 7120
  • ALPN: curl offers h2,http/1.1
  • (304) (OUT), TLS handshake, Client hello (1):
  • (304) (IN), TLS handshake, Server hello (2):
  • TLSv1.2 (IN), TLS handshake, Certificate (11):
  • TLSv1.2 (IN), TLS handshake, Server key exchange (12):
  • TLSv1.2 (IN), TLS handshake, Server finished (14):
  • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
  • TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
  • TLSv1.2 (OUT), TLS handshake, Finished (20):
  • TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
  • TLSv1.2 (IN), TLS handshake, Finished (20):
  • SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384 / [blank] / UNDEF
  • ALPN: server accepted h2
  • Server certificate:
  • subject: CN=localhost
  • start date: Nov 22 21:46:11 2024 GMT
  • expire date: Nov 22 21:46:11 2025 GMT
  • issuer: CN=localhost
  • SSL certificate verify ok.
  • using HTTP/2
  • [HTTP/2] [1] OPENED stream for https://localhost:7120/swagger/index.html
  • [HTTP/2] [1] [:method: GET]
  • [HTTP/2] [1] [:scheme: https]
  • [HTTP/2] [1] [:authority: localhost:7120]
  • [HTTP/2] [1] [:path: /swagger/index.html]
  • [HTTP/2] [1] [user-agent: curl/8.7.1]
  • [HTTP/2] [1] [accept: /]

GET /swagger/index.html HTTP/2
Host: localhost:7120
User-Agent: curl/8.7.1
Accept: /

  • Request completely sent off
    < HTTP/2 404
    < date: Wed, 11 Dec 2024 08:30:14 GMT
    < server: Kestrel
    < content-length: 0
    <
  • Connection #0 to host localhost left intact

I hope you can reopen this one.

@fabiogoma
Copy link

fabiogoma commented Dec 14, 2024

This issue is still happening. My current SDK version is: 9.0.101

To reproduce the issue follow the VS Code steps from: Tutorial: Create a web API with ASP.NET Core

To make it shorter, run:

Create a new project

dotnet new webapi --use-controllers -o TodoApi cd TodoApi dotnet add package Microsoft.EntityFrameworkCore.InMemory code -r ../TodoApi

Generate and trust self-signed certificates

dotnet dev-certs https --trust

Start the app

dotnet run --launch-profile https

When you start the application and try to reach the swagger on your browser, you get a blank page. Via curl the output is: % curl -k -vvv https://localhost:7120/swagger/index.html

  • Host localhost:7120 was resolved.
  • IPv6: ::1
  • IPv4: 127.0.0.1
  • Trying [::1]:7120...
  • Connected to localhost (::1) port 7120
  • ALPN: curl offers h2,http/1.1
  • (304) (OUT), TLS handshake, Client hello (1):
  • (304) (IN), TLS handshake, Server hello (2):
  • TLSv1.2 (IN), TLS handshake, Certificate (11):
  • TLSv1.2 (IN), TLS handshake, Server key exchange (12):
  • TLSv1.2 (IN), TLS handshake, Server finished (14):
  • TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
  • TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
  • TLSv1.2 (OUT), TLS handshake, Finished (20):
  • TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
  • TLSv1.2 (IN), TLS handshake, Finished (20):
  • SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384 / [blank] / UNDEF
  • ALPN: server accepted h2
  • Server certificate:
  • subject: CN=localhost
  • start date: Nov 22 21:46:11 2024 GMT
  • expire date: Nov 22 21:46:11 2025 GMT
  • issuer: CN=localhost
  • SSL certificate verify ok.
  • using HTTP/2
  • [HTTP/2] [1] OPENED stream for https://localhost:7120/swagger/index.html
  • [HTTP/2] [1] [:method: GET]
  • [HTTP/2] [1] [:scheme: https]
  • [HTTP/2] [1] [:authority: localhost:7120]
  • [HTTP/2] [1] [:path: /swagger/index.html]
  • [HTTP/2] [1] [user-agent: curl/8.7.1]
  • [HTTP/2] [1] [accept: /]

GET /swagger/index.html HTTP/2
Host: localhost:7120
User-Agent: curl/8.7.1
Accept: /

  • Request completely sent off
    < HTTP/2 404
    < date: Wed, 11 Dec 2024 08:30:14 GMT
    < server: Kestrel
    < content-length: 0
    <
  • Connection #0 to host localhost left intact

I hope you can reopen this one.

I've made a mistake while following the tutorial using the latest version (.NET 9), it is now known that .NET 9 removed swagger support from the templates, so it makes sense getting this 404 when calling it.

More info can be found at: https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-8.0&tabs=visual-studio

The current is status is that I managed to get it working using the latest release of .NET 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc-enhancement seQUESTered Identifies that an issue has been imported into Quest. Source - Docs.ms Docs Customer feedback via GitHub Issue
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.