I am working through the Microsoft Learn tutorials to "Create a web API with ASP.Net Core".
Under the heading, "Build and test the web API", at instruction (5) I am getting a response, "Unable to find an OpenAPI description".
For step (6) when executing the "ls" command I get the response, "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 have tried the "connect" command suggested here and have tried "dir" as an alternative to "ls".
I can successfully change directories in step (7) and execute the GET request for step (8) and receive the expected reply. However, it really bothers me the "ls" command is not working here and seems like an important function of the httprepl tool.
How can I get the "ls" command to work here or tell me why does it not work?
C:UsersxxxxsourcereposLearnContosoPizza>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.
http://localhost:5000/>
ADDED RESULTS OF SUGGESTIONS–
C:UsersxxxxsourcereposLearnContosoPizza>dotnet --version
3.1.412
C:UsersxxxxsourcereposLearnContosoPizza>dotnet add WebAPI.csproj package Swashbuckle.AspNetCore -v 5.6.3
Could not find project or directory `WebAPI.csproj`.
4
Answers
In step 5
HttpRepl
emits the warningUnable to find an OpenAPI description
, which means that it can’t find the swagger endpoint, and therefore thels
command wont work.I assume you are using VS Code and ASP.NET Core 5.0. Here is my output from running
dotnet --version
:If we are using Visual Studio, then remember to enable swagger when you create the project – I am using Visual Studio 2019 to create the screenshot:
Specifying your OpenAPI description
To find out which endpoint to use, open the file
Startup.cs
and locate the code fragment that contains the textUseSwaggerUI
. You should find this block of code:Use the endpoint you find and run the tool like this:
If you do not find any references to swagger, then see None of the above worked, swagger isn’t installed below, for how to install and configure swagger for your project.
Ignoring your environment
If specifying the Open API endpoint to use doesn’t work, then you are not running your Web API in a development environment. So either use a development environment, or uncomment the if-statement while testing (to setup your environment for development, see Changing your environment below):
Remember to restore the code you uncommented, if any, before you deploy to production.
Changing your environment
The profile your Web API is using, is specified in the file
PropertieslaunchSettings.json
. Open the file and search forASPNETCORE_ENVIRONMENT
. Then change the instances you find to:If this doesn’t work, or the instances were already set to "Development", it means that you are not using any of the profiles specified in your launch settings. If no profile is used,
ASPNETCORE_ENVIRONMENT
defaults to "Production". When using thedotnet run
command, the--launch-profile
parameter lets you specify which profile to use:As a last resort you can set the environment variable
ASPNETCORE_ENVIRONMENT
in the shell you are using, before you run the commanddotnet run
:Bash
CMD
PowerShell
Then run the application without a profile :
The default ports, when running without a profile, should be 5000 or 5001. But read the output from the command, to see which ports it assigns to your Web API.
Please note, if you use VS Code to run your project, that VS Code may also have created launch settings in the
.vscodelaunch.json
. It depends on how you have configured VS Code and what you allow it to do. I found some older articles, that claim that some extensions for VS Code, may interfere with the launch settings, but they didn’t specify which ones.None of the above worked, swagger isn’t installed
I none of the above worked, it means you don’t have swagger installed. Install swagger for your project and when done, try again.
Package Installation
Open your project in VS Code and run the following command from the Integrated Terminal and replace WebAPI.csproj with the name of your own project file:
You can of course run the command from outside VS Code, with your project folder as the current working directory.
Add and configure Swagger middleware
Add the Swagger generator to the services collection in the
Startup.ConfigureServices
method, as the last statement in the method:In the
Startup.Configure
method, enable the middleware for serving the generated JSON document and the Swagger UI, at the top of the method:To learn more about setting up swagger, profiles and the environment
Get started with Swashbuckle and ASP.NET Core
Managing Production and Development Settings in ASP.NET Core
Use multiple environments in ASP.NET Core
ASP.NET Core web API documentation with Swagger / OpenAPI
The solution for me was to simply trust localhost’s SSL certification, which you can do with this command:
While doing the same Tutorial, a friend of mine noticed, that trusting the dev certificate, was already covered by the Tutorial, which I had overlooked doing the Tutorial myself. This is the official help site:
Trust the ASP.NET Core HTTPS development certificate on Windows and macOS.
Maybe this will still help someone with the same problem.
You must be connected to the web server through dotnet run.
I had faced same issue. I have solved it by following:
You should be able to run api now.