Reputation: 1573
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
Upvotes: 5
Views: 18348
Reputation: 755
Fully qualify it like this:
var builder = Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(args);
Upvotes: 16
Reputation: 291
This also happens if you create a project and the Project SDK is not a Web SDK
In your project file:
Incorrect:
<Project Sdk="Microsoft.NET.Sdk">
Correct:
<Project Sdk="Microsoft.NET.Sdk.Web">
Upvotes: 17
Reputation: 11
I had the same message but this disappeared after changing the TargetPackage from net7.0 to net6.0 in the cproj file
<TargetFramework>net6.0</TargetFramework>
Upvotes: 0
Reputation: 303
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>
Upvotes: 15