I got up and running with Visual Studio 2022 Preview for a couple of days now.
Got the first shock, there is no Startup.cs. Thats ok, a bit of reading, I know Startup is removed.
Today got another slap. I see no using statements. Here it is.
I just created a brand new .NET 6 web app and as I hover over the WebApplication class, I realized it stays in Microsoft.AspNetCore.Builder namespace. And the generated Program.cs class looks like this.
So where is the using Microsoft.AspNetCore.Builder;
statement?
Whats the magic? Why is .net becoming mystical by the day?
The full Program.cs file is as follows.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
C# 10.0 introduces a new feature called global using directive (global using <fully-qualified-namespace>;
) which allows to specify namespaces to be implicitly imported in all files in the compilation. .NET 6 RC1 has this feature enabled by default in new project templates (see the <ImplicitUsings>enable</ImplicitUsings>
property in your .csproj).
For Microsoft.NET.Sdk.Web
next namespaces should be implicitly imported (plus the ones from Microsoft.NET.Sdk
):
UPD
To address your questions in the comment:
At the moment of writing the generated file containing default imports will be inside the obj
folder named something like ProjectName.GlobalUsings.g.cs
.
To modify default imports you can add Using
element to your .csproj
file. Based on exposed attributes it allows several actions including addition and removal:
<ItemGroup>
<Using Include="SomeFullyQualifiedNamespace"/>
</ItemGroup>
For just addition you can simply prefix your using
directive with global
modifier in any file (or create a separate one just for this):
global using SomeFullyQualifiedNamespace;