Reputation: 389
I tried to create a new Web API in ASP.NET Core 6.0 as shown https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio-code#create-a-web-project.
But it is created with errors.
This is the WeatherForecastController
of my new project:
using Microsoft.AspNetCore.Mvc;
// see here This is ends with ";". And showing this error "{ expected [TodoApi]"
namespace TodoApi.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
// some code
}
And the WeatherForecast
class looks like this
// see here This is ends with ";". And showing this error "{ expected [TodoApi]"
namespace TodoApi;
public class WeatherForecast
{
// some code
}
And all namespaces are missing.
program.cs
file looks like this; it does not have any namespace when created:
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();
//.......
And TodoApi.GlobalUsings.g.cs
file also has errors
// <auto-generated/>
global using global::Microsoft.AspNetCore.Builder;
global using global::Microsoft.AspNetCore.Hosting;
global using global::Microsoft.AspNetCore.Http;
global using global::Microsoft.AspNetCore.Routing;
global using global::Microsoft.Extensions.Configuration;
global using global::Microsoft.Extensions.DependencyInjection;
global using global::Microsoft.Extensions.Hosting;
global using global::Microsoft.Extensions.Logging;
global using global::System;
global using global::System.Collections.Generic;
global using global::System.IO;
global using global::System.Linq;
global using global::System.Net.Http;
global using global::System.Net.Http.Json;
global using global::System.Threading;
global using global::System.Threading.Tasks;
The output of dotnet --info
is
.NET SDK (reflecting any global.json):
Version: 6.0.100
Commit: 9e8b04bbff
Runtime Environment:
OS Name: ubuntu
OS Version: 20.04
OS Platform: Linux
RID: ubuntu.20.04-x64
Base Path: /snap/dotnet-sdk/147/sdk/6.0.100/
Host (useful for support):
Version: 6.0.0
Commit: 4822e3c3aa
.NET SDKs installed:
6.0.100 [/snap/dotnet-sdk/147/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.0 [/snap/dotnet-sdk/147/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.0 [/snap/dotnet-sdk/147/shared/Microsoft.NETCore.App]
To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download
But if I create an ASP.NET Core 3.1 project, it does not show these errors (I installed ASP.NET Core 3.1 to create ASP.NET Core 3.1 Web API).
I don't know why these errors occurs when creating a new ASP.NET Core 6.0 Web API project.
Does anyone know ?
Edit : I am using visual studio code. and TodoApi.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>
Upvotes: 1
Views: 2934
Reputation: 17622
There are several new features of C# at play here.
First of all in Program.cs the Program class is "missing" because of a new feature called "Top level programs".
https://devblogs.microsoft.com/dotnet/c-9-0-on-the-record/#top-level-programs
The missing namespaces are because of "Global and implicit usings" which give you some standard namespaces included globally.
https://devblogs.microsoft.com/dotnet/announcing-net-6/#global-using-directives
As for the namespace with a semicolon after that is a "File scoped namespace" which is the same as wrapping the whole file in a namespace but without needing to indent everything.
https://devblogs.microsoft.com/dotnet/announcing-net-6/#file-scoped-namespaces
If you are using Visual Studio 2019 you will need to update to Visual Studio 2022 to use all these features. VSCode should automaticly update the C# extension to the latest version but if it hasn't (or you have disabled automatic updates) you might need to update it manually.
https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp
Upvotes: 1