ASP.NET Core Web API - The name 'WebApplication' does not exist in the current context WebApi

Viewed 1563

In ASP.NET Core-6 Web API, I have this code in Program.cs of the WebApi Project:

WebApi Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();

I got this error:

Error CS0103 The name 'WebApplication' does not exist in the current context WebApi

How do I resolve this?

Thanks

1 Answers

Usually this happens when you have updated a .Net < 5 app to .Net 6. The reason is that you have to enable ImplicitUsings in the .csproj file

Makes sure the property group for your .csproj file contains this line in it:

<ImplicitUsings>enable</ImplicitUsings>

Here is an example PropertyGroup from one of the recent projects where I upgraded from .Net Core 3.1 to .Net 6:

<PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
Related