c# - 找不到 OpenAPI 描述

标签 c# .net nuget webapi

我正在学习 Microsoft Learn 教程“使用 ASP.Net Core 创建 Web API”。

我使用.NET5

运行命令时遇到问题:

httprepl connect http://localhost:5000

我收到回复“无法找到 OpenAPI 描述”。

下面的命令“ls”从它返回我而不是端点。

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.

我在@Nuse Why is HttpRepl unable to find an OpenAPI description? The command "ls" does not show available endpoints发起的问题中尝试了@Gowiser提供的所有解决方案

但是没有任何效果。

最佳答案

正在处理Create a web API with ASP.NET Core Controllers我也遇到了错误

Unable to find an OpenAPI description

本教程使用 dotnet 6,我使用的是 dotnet core 3.1。

我做了更改from sasha answers here并且必须添加这些包

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

毕竟我仍然收到错误消息。在Exercise - Create a web API project的第4步中以下是这样写的

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}

我不知道像这样运行httprepl

httprepl https://localhost:{PORT}

仍然需要您的网络应用程序正在运行。因此,您必须打开第二个终端 (cmd.exe) 来运行您的 webapi 项目 dotnet run。之后,您可以像这样使用 httprepl 连接到 swagger 文档

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

这对我有用。

添加 swagger ui

Swashbuckle.AspNetCore - Getting Started包含

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");
});

如果您更改 startup.cs 以包含此内容

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"));
    }            
    ....

你会在http://localhost:5000/swagger/下得到一个漂亮的GUI,看起来像这样

Swagger ui

关于c# - 找不到 OpenAPI 描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69571770/

相关文章:

c# - 什么在 NuGet 包中调用 Start() 方法(例如 :AspNet. ScriptManager.jQuery)

c# - 为什么我的下拉列表没有填满文本框?

C# 从异步任务返回不起作用

.net - 在 C# 4.0 中为可选参数提供默认值

.net - ssl 证书验证绕过不会在 Windows Server 2016 上触发

visual-studio - 未知选项 -RequireConsent

c# - 如何使用 linq 按集合中的属性排序?

c# - List<string> 获取特定字符串的方法

c# - WPF - 更改对象属性时通知 ViewModel

git - 迁移到 NuGet 自动包还原