admin管理员组文章数量:1315831
I am trying to use the OpenApi
package in my minimal API project following this tutorial from the Microsoft documentation.
I am using Ubuntu 24.04, and I was able to successfully install the library by running the command:
dotnet add package Microsoft.AspNetCore.OpenApi
The problem comes when i try to use it:
using Microsoft.AspNetCore.OpenApi; /* Error here: The type or namespace name 'OpenApi' does not exist in the namespace 'Microsoft.AspNetCore'*/
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
builder.Services.AddOpenApi(); /* Error here: 'IServiceCollection' does not contain a definition for 'AddOpenApi' and no accessible extension method 'AddOpenApi' accepting a first argument of type 'IServiceCollection' could be found */
app.MapGet("/", () => "Hello World!");
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); /*Error here: 'WebApplication' does not contain a definition for 'MapOpenApi' and no accessible extension method 'MapOpenApi' accepting a first argument of type 'WebApplication' could be found*/
}
app.Run();
So I am not sure what I am missing, I am using the 9 version and my csproj
file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
</ItemGroup>
</Project>
Any ideas what I am missing?
I am trying to use the OpenApi
package in my minimal API project following this tutorial from the Microsoft documentation.
I am using Ubuntu 24.04, and I was able to successfully install the library by running the command:
dotnet add package Microsoft.AspNetCore.OpenApi
The problem comes when i try to use it:
using Microsoft.AspNetCore.OpenApi; /* Error here: The type or namespace name 'OpenApi' does not exist in the namespace 'Microsoft.AspNetCore'*/
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
builder.Services.AddOpenApi(); /* Error here: 'IServiceCollection' does not contain a definition for 'AddOpenApi' and no accessible extension method 'AddOpenApi' accepting a first argument of type 'IServiceCollection' could be found */
app.MapGet("/", () => "Hello World!");
if (app.Environment.IsDevelopment())
{
app.MapOpenApi(); /*Error here: 'WebApplication' does not contain a definition for 'MapOpenApi' and no accessible extension method 'MapOpenApi' accepting a first argument of type 'WebApplication' could be found*/
}
app.Run();
So I am not sure what I am missing, I am using the 9 version and my csproj
file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.1" />
</ItemGroup>
</Project>
Any ideas what I am missing?
Share Improve this question asked Jan 30 at 2:56 roderknightroderknight 1191 gold badge2 silver badges8 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 3Swagger has been removed in .NET 9, and we should use the following package instead:
dotnet add package Swashbuckle.AspNetCore
And in Program.cs, we should update the code to the following:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapGet("/", () => "Hello World!");
app.Run();
本文标签: cCannot use OpenApi in minimal API projectStack Overflow
版权声明:本文标题:c# - Cannot use OpenApi in minimal API project - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987928a2408795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
dotnet add package
does this exact thing already – Michał Turczyn Commented Jan 30 at 8:17