AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

macOS - no Swagger on Tutorial: Create a web API

Open downshiftdata opened this issue 5 years ago • 29 comments

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

downshiftdata avatar Mar 01 '21 14:03 downshiftdata

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

image

Rick-Anderson avatar Mar 02 '21 00:03 Rick-Anderson

I have .NET 5 installed and was having the same problem. I fixed it by going to Project Properties -> Debug and setting Hosting Model to "Out of Process".

philiptkd avatar Mar 02 '21 21:03 philiptkd

I have .NET 5 installed and was having the same problem. I fixed it by going to Project Properties -> Debug and setting Hosting Model to "Out of Process".

That has nothing to do with Swagger endpoints.

Perhaps you unchecked Enable OpenAPI support

image

Rick-Anderson avatar Mar 03 '21 01:03 Rick-Anderson

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

image

this solution worked for me

rajalaxmijena avatar Apr 29 '21 00:04 rajalaxmijena

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

drock702 avatar Jun 24 '21 05:06 drock702

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 avatar Jun 26 '21 04:06 paulzolnierczyk

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

drock702 avatar Jun 27 '21 03:06 drock702

@wadepickett can you test this on macOS?

Rick-Anderson avatar Jun 27 '21 03:06 Rick-Anderson

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.

rsoaresgouveia avatar Aug 19 '21 00:08 rsoaresgouveia

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

SwaraliJoshi avatar Aug 21 '21 16:08 SwaraliJoshi

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 :(

paroqa02 avatar Sep 01 '21 21:09 paroqa02

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.

rsoaresgouveia avatar Sep 01 '21 22:09 rsoaresgouveia

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.

twakil avatar Sep 15 '21 18:09 twakil

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.

Rick-Anderson avatar Sep 15 '21 19:09 Rick-Anderson

That's nice, thanks I will try all of your suggestions.

Thanks

Paramita Roy

Senior Quality Assurance Analyst II (IT Analysis)

Mobile: +1 682-300-6222

https://www.linkedin.com/in/paramita-roy-297197104/

On Wed, Sep 15, 2021 at 3:04 PM Rick Anderson @.***> wrote:

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.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dotnet/AspNetCore.Docs/issues/21647#issuecomment-920300287, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP56D5AEPSJ2LK3VHVEYRE3UCDU4BANCNFSM4YMU5C3Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

paroqa02 avatar Sep 16 '21 14:09 paroqa02

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

yiruli avatar Oct 23 '21 15:10 yiruli

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.

shadrachdev avatar Oct 26 '21 18:10 shadrachdev

Awesome, thanks!

On Tue, Oct 26, 2021 at 2:03 PM Shad @.***> wrote:

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: [image: Screen Shot 2021-09-01 at 18 51 57] https://user-images.githubusercontent.com/12357601/131756038-47edcef4-d9d0-4e71-8d44-58e8cfabf7cb.png

I hope it helps.

This fixed the problem for me. VS Mac on 2020 MBP.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/dotnet/AspNetCore.Docs/issues/21647#issuecomment-952181108, or unsubscribe https://github.com/notifications/unsubscribe-auth/AP56D5DTX4KECKVPK2C2GKLUI3UNRANCNFSM4YMU5C3Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

-- Paramita Roy Software Quality Assurance Analyst Contact No: 682-300-6222

paroqa02 avatar Oct 26 '21 18:10 paroqa02

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

rohitsiddha avatar Nov 23 '21 13:11 rohitsiddha

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

Rick-Anderson avatar Nov 23 '21 17:11 Rick-Anderson