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
  • 2 can't reproduce it on Windows10 and VS 2022. Try Clean/Rebuild/Restore – Ivan Petrov Commented Jan 30 at 3:07
  • I suggest you could install a vscode and use its run and reinstall the package to see if this issue is exists or not – Brando Zhang Commented Jan 30 at 6:50
  • 1 @BrandoZhang Not sure that instaliing one IDE or another would help with installing packages. dotnet add package does this exact thing already – Michał Turczyn Commented Jan 30 at 8:17
  • How do you build the project? From IDE or CLI? – Guru Stron Commented Jan 30 at 8:30
  • I am using the CLI to build, run, or add packages, and just use VSCode for debugging and write code – roderknight Commented Jan 31 at 3:18
 |  Show 1 more comment

1 Answer 1

Reset to default 3

Swagger 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