1

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. The screenshot of 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>

8
  • You probably have an old Visual Studio version. Install 2022 for proper C# 10 support. Or update the C# plugin for VS Code if you're using that. Commented Nov 11, 2021 at 13:58
  • 1
    What language version is set in csproj? Commented Nov 11, 2021 at 13:58
  • 1
    The namespaces and Program class missing is perfectly fine. It is a new feature (or a few new features) in C# 10 and .NET 6. Commented Nov 11, 2021 at 13:59
  • Please read How to Ask and update your title and body. "Issues" and "errors" do not help us solve the issue, nor the fact that the only proof of you using Visual Studio Code is in one of the links you posted. Mention the exact errors and software used. See also my first comment. Commented Nov 11, 2021 at 14:08
  • it is created with errors. You didn't post any errors. .NET 6 works just fine - I've been using it since Preview 1 for ASP.NET Core Web API and Blazor WASM projects Commented Nov 11, 2021 at 14:14

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

6 Comments

"If you are using VSCode and the commandline tools it should be fine though" - no, VS Code needs an update of the OmniSharp C# plugin as well to support the new syntax.
Yes, but since VSCode autoupdates extensions that probably won't be a problem.
If it did, OP wouldn't have this problem. They disabled auto-updates or didn't restart VS Code or don't have internet access or whatever.
Thank you @Karl-JohanSjögren
But When I create a new .cs class , It is created like this namespace TechnePaymentApi.Models { public class OpayoPayment { } } Is it ok in .net core 6.0 project?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.