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.
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:
services.AddSwaggerGen() call in the Startup.ConfigureServices() methodapp.UseSwagger() call in the Startup.Configure() methodHere 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.
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 above 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.
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
run this command first:
dotnet dev-certs https --trust