2

I am working the Microsoft Learn tutorials to "Create a web API with ASP.Net Core".

I used .NET5

I have a problem when I run the command:

httprepl connect http://localhost:5000

I am getting a response, "Unable to find an OpenAPI description".

And the following command "ls" returns me from it not trooping endpoints.

c:/xxx/Source/Repos/ContosoPizza
$ httprepl http://localhost:5000
(Disconnected)> connect http://localhost:5000
Using a base address of http://localhost:5000/
Unable to find an OpenAPI description
For detailed tool info, see https://aka.ms/http-repl-doc

http://localhost:5000/> ls
No directory structure has been set, so there is nothing to list. Use the "connect" command to set a directory structure based on an OpenAPI description.

I tried all the solutions offered by @Gowiser in the question launched by @Nuse Why is HttpRepl unable to find an OpenAPI description? The command "ls" does not show available endpoints

But nothing worked.

Artkanoid
  • 105
  • 1
  • 14
  • What exactly have you tried? You said you tried everything suggested in the linked Q&A, but how exactly did you try it? What's your code to add and use Swagger in your `Startup.cs`? – MindSwipe Oct 14 '21 at 13:43

4 Answers4

3

Working through Create a web API with ASP.NET Core Controllers i also did run into the error

Unable to find an OpenAPI description

The tutorial uses dotnet 6, i was using dotnet core 3.1.

I did the changes from sasha answers here and had to add these packages

dotnet add package Microsoft.OpenApi --version 1.2.3 
dotnet add package Swashbuckle.AspNetCore.Swagger --version 6.2.3 
dotnet add package Swashbuckle.AspNetCore.SwaggerUI --version 6.2.3 
dotnet add package Swashbuckle.AspNetCore.SwaggerGen --version 6.2.3

After all that i still got error messages. In step 4 of Exercise - Create a web API project the following is written

Connect to our web API by running the following command:

httprepl https://localhost:{PORT}

Alternatively, run the following command at any time while the HttpRepl is running:

(Disconnected)> connect https://localhost:{PORT}

I was not aware that running httprepl like this

httprepl https://localhost:{PORT}

still requires that your web app is running. So you have to open a second terminal (cmd.exe) to run your webapi project dotnet run. After that you can connect to the swagger docs using httprepl like this

connect http://localhost:5000 --verbose --openapi /swagger/v1/swagger.json

http://localhost:5000/> ls                           
.                 []
WeatherForecast   [GET]

http://localhost:5000/> cd WeatherForecast
/WeatherForecast    [GET]

http://localhost:5000/WeatherForecast> get
HTTP/1.1 200 OK

This did the trick for me.

Adding the swagger ui

Swashbuckle.AspNetCore - Getting Started contains

Optionally, insert the swagger-ui middleware if you want to expose interactive documentation, specifying the Swagger JSON endpoint(s) to power it from.

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});

If you change startup.cs to include this

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "ContosoPizza v1"));
    }            
    ....

You get a nice gui under http://localhost:5000/swagger/ which looks like this

Swagger ui

surfmuggle
  • 5,527
  • 7
  • 48
  • 77
  • Wow. So it does! Works great. The MS tutorial had the UseSwagger() methods commented out (don't know why). Un-commenting them and doing the URL above revealed a nice Postman like view of the project. I didn't know this. Thanks! – Fandango68 Apr 12 '22 at 04:18
1

I am assuming you are following the steps described in the 'Create a web API project' exercise of the Create a web API with ASP.NET Core course.

Before going through step 5. make sure in your project there is:

  • a services.AddSwaggerGen() call in the Startup.ConfigureServices() method
  • an app.UseSwagger() call in the Startup.Configure() method

Here is a working version of Startup.cs:

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 ContosoPizza
{
    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 = "ContosoPizza", 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", "ContosoPizza v1"));
            }

            app.UseRouting();

            app.UseAuthorization();

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

Calling ls in the httprepl session shows expected results:

❯ httprepl http://localhost:5000
(Disconnected)> connect http://localhost:5000
Using a base address of http://localhost:5000/
Using OpenAPI description at http://localhost:5000/swagger/v1/swagger.json
For detailed tool info, see https://aka.ms/http-repl-doc

http://localhost:5000/> ls
.                 []
WeatherForecast   [GET]

http://localhost:5000/>

Here you can find a full project for this exercise.

Sasha
  • 827
  • 1
  • 9
  • 16
0

Indeed @surfmuggle to connect to the API via httprepl, the API had to be running in another terminal, so 2 terminals in total:

  • 1 to run the API with dotnet run
  • 1 other to call instructions like ls or other later
Artkanoid
  • 105
  • 1
  • 14
-1

run this command first:

dotnet dev-certs https --trust
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 30 '21 at 08:25