19

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:\Users\xxxx\source\repos\Learn\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.       

http://localhost:5000/>

ADDED RESULTS OF SUGGESTIONS--

C:\Users\xxxx\source\repos\Learn\ContosoPizza>dotnet --version
3.1.412

C:\Users\xxxx\source\repos\Learn\ContosoPizza>dotnet add WebAPI.csproj package Swashbuckle.AspNetCore -v 5.6.3
Could not find project or directory `WebAPI.csproj`.

httprepl GitHub repo and MS Docs page

Nuse
  • 193
  • 1
  • 1
  • 8
  • I followed the directions of the tutorial but it did not tell me to prepare any OpenAPI description not really sure what that is just yet. I still need to look it up. I saw on an introduction to HttpRepl video by IAmTimCorey on youtube he checked an OpenAPI box in Visual Studio 2019, but I am using VSCode and command line. – Nuse Sep 22 '21 at 06:15
  • The OpenAPI is already configured in the template you use. I have tested the tutorial with both the command line and with the Visual Studio GUI, and it works. I don't know why it doesn't work for you. But I have several suggestions you can try below. – GoWiser Sep 22 '21 at 11:56
  • Maybe also add the output of the following command ```dotnet --version```, so we know what version of .net we are working with. – GoWiser Sep 24 '21 at 02:07
  • @Gowiser Thanks for your guidance on this. Looks like I may have to use a more recent version of dotnet framework for this project I have been using dotnet core 3.1 LTS version and this is the first trouble I've come across. – Nuse Sep 25 '21 at 03:05
  • The solution is to install the Swashbuckle package, then configure it as shown in this link: https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-5.0&tabs=visual-studio – Flavio Rios Oct 19 '21 at 18:33
  • @FlavioRios I think the solution was that he needed to update his environment and restart from scratch. – GoWiser Nov 05 '21 at 11:19

4 Answers4

38

The solution for me was to simply trust localhost's SSL certification, which you can do with this command:

dotnet dev-certs https --trust

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.

yuzepe
  • 481
  • 1
  • 3
  • 4
  • 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 Nov 15 '21 at 14:41
  • The tutorial mentioned in the question, doesn't use developer certificates. – GoWiser Nov 23 '21 at 01:05
  • 2
    @GoWiser Actually it does in my understanding, if you look in unit 3 at the first blue "Important" box, where it is noted. Please correct me, if I am wrong! – yuzepe Nov 26 '21 at 18:31
  • His error is *Unable to find an OpenAPI description*. So it has nothing to do with auth. – GoWiser Jun 14 '22 at 09:22
  • 2
    I have just gone through this and was receiving the same message when using https but not while using http. Specifying the endpoint with `https://localhost:7140 --openapi /swagger/v1/swagger.json` returns the error `The SSL connection could not be established, see inner exception.`. Trusting the ASP.NET developer cert has resolved the issue. – Dan-di-Lion Jun 20 '22 at 18:38
15

In step 5 HttpRepl emits the warning Unable to find an OpenAPI description, which means that it can't find the swagger endpoint, and therefore the ls command wont work.

I assume you are using VS Code and ASP.NET Core 5.0. Here is my output from running dotnet --version:

5.0.401

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:

enter image description here

Specifying your OpenAPI description

To find out which endpoint to use, open the file Startup.cs and locate the code fragment that contains the text UseSwaggerUI. You should find this block of code:

if (env.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI v1"));
}

Use the endpoint you find and run the tool like this:

httprepl http://localhost:5000 --openapi /swagger/v1/swagger.json

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

//if (env.IsDevelopment())
//{
    app.UseDeveloperExceptionPage();
    app.UseSwagger();
    app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI v1"));
//}

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 Properties\launchSettings.json. Open the file and search for ASPNETCORE_ENVIRONMENT. Then change the instances you find to:

"ASPNETCORE_ENVIRONMENT": "Development"

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 the dotnet run command, the --launch-profile parameter lets you specify which profile to use:

dotnet run --launch-profile "name_of_profile"

As a last resort you can set the environment variable ASPNETCORE_ENVIRONMENT in the shell you are using, before you run the command dotnet run:

Bash

export ASPNETCORE_ENVIRONMENT=Development

CMD

set ASPNETCORE_ENVIRONMENT=Development

PowerShell

$env:ASPNETCORE_ENVIRONMENT='Development'

Then run the application without a profile :

dotnet run --no-launch-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 .vscode\launch.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

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

dotnet add WebAPI.csproj package Swashbuckle.AspNetCore -v 5.6.3

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:

public void ConfigureServices(IServiceCollection services)
{
    [... other code here ...]

    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPI", Version = "v1" });
    });

}

In the Startup.Configure method, enable the middleware for serving the generated JSON document and the Swagger UI, at the top of the method:

public void Configure(IApplicationBuilder app)
{
    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.),
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });

    [... other code here for setting up routing and the like ...]
}

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

GoWiser
  • 857
  • 6
  • 20
  • 1
    Thanks this was all very informative and gives me alot to do more research, but unfortunately Startup.cs contains the following code auto generated by the "dotnet new webapi --no-https" command. if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } and my ASPNETCORE_ENVIRONMENT: in the launchSettings.json file is set to "Development". – Nuse Sep 23 '21 at 07:35
  • @Nuse I have modified the answer, to help you with adding and configuring swagger for your webapi, inlcuding a link to the official documentation. I am curious why it didn't work for you out of the box, so if you find out, then please let me know. – GoWiser Sep 24 '21 at 02:05
  • Thanks for your assistance. Upgrading to .NET 5 fixed the issue. Starting out with some slightly dated materials, I was not aware Microsoft had dropped the Core and Framework monikers and that .NET 5 is the next version of Core while abandoning Framework. (At least that is what I think I read please correct me if I am wrong) – Nuse Sep 25 '21 at 04:54
  • I think you are correct. Microsoft will drop support for older versions of .Net, except .Net 4.8. So we should stick to using .Net 4.8, .Net5 (current), .Net6 and in the future .Net7. I may be mistaken as Microsoft doesn't always create the most detailed and up-to-date information. I currently use .Net 4.8 to maintain older applications and .Net5 or .Net6 for new applications. – GoWiser Nov 23 '21 at 01:00
  • My problem was that I was missing the Startup.cs file in the package I downloaded following the link on the Microsoft Learn tutorial and that I wasn't running in development mode. Thanks @GoWiser for such detail answer. – KYLO Apr 04 '22 at 15:17
0

I had faced same issue. I have solved it by following:

  1. In Developer PowerShell(VS 2022), Run 'dotnet run' command.

enter image description here

  1. Keep this powershell as it is.
  2. Now open new PowerShell and run "httprepl https://localhost:{PORT}"

You should be able to run api now.

enter image description here

prisan
  • 1,251
  • 12
  • 9
-1

You must be connected to the web server through dotnet run.

  • 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 Nov 24 '21 at 06:20