ASP.NET Core 6.0 - Global Usings

Ken Haggerty
Created 01/05/2022 - Updated 01/12/2022 02:32 GMT

This article describes the new VS 2022 features, Global using directives and file scoped namespaces. I will assume you have downloaded the ASP.NET Core 6.0 - Users With Device 2FA Project.

Users With Device 2FA Project and Article Series

This article series is about the implementation of ASP.NET Core 6.0 with Visual Studio 2022. The ASP.NET Core 6.0 - Users With Device 2FA Project (UWD2FAP) implements WebAuthn, also known as FIDO2, instead of authenticator apps for two-factor authentication (2FA). The project implements Bootstrap v5 and Bootstrap Native. After a user registers, they can enable 2FA with Windows Hello, Android Lock Screen, or a FIDO2 security key. If I had this project when I created KenHaggerty. Com three years ago, I would have started with this user authentication template. The latest version is published at Preview. KenHaggerty. Com. I encourage you to register and evaluate multiple 2FA devices in Manage Account > Two-Factor Authentication. Details, screenshots, and related articles can be found at ASP.NET Core 6.0 - Users With Device 2FA Project. The details page includes the version change log.

Visual Studio 2022 (VS 2022) is required to develop .NET 6 and ASP.NET Core 6.0 applications. VS 2022 Downloads.

A global using directive imports a namespace for your whole application instead of a single file. These global directives can be added either by adding a <Using> item to the project file, or by adding the global using directive to a code file. See MSDocs - Global using directives.

File scoped namespaces use a less verbose format for the typical case of files containing only one namespace. The file scoped namespace format is namespace X.Y.Z; (note the semicolon and lack of braces). This allows for files like the following: See MSDocs - File Scoped Namespaces.

Implementing the new Global using directives and file scoped namespaces features allows less ceremony and better code clarity for single namespace files.

Before:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
        }

        public void OnGet()
        {

        }
    }
}
After:
namespace WebApplication1.Pages;
public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;

    public IndexModel(ILogger<IndexModel> logger)
    {
        _logger = logger;
    }

    public void OnGet()
    {

    }
}

When you create a new ASP.NET Core 6.0 Web App with VS 2022, a new feature, Implicit global usings is enabled by default and a <PROJECTNAME>. GlobalUsing. cs file is generated in the project SDK folder.

Project Properties Implicit Global Usings
Solution Explorer Generated Global Usings

The <PROJECTNAME>. GlobalUsing. cs file is auto generated and only implements a few Microsoft and System namespaces. You can implement Global using directives from any code file by declaring the using as global. The UWD2FAP implements a GlobalUsing.cs file to store and manage global usings. See Introducing C# 10: Global usings, example with ASP.NET Core 6. The UWD2FAP disables Implicit global usings but the usings declared by Implicit global usings is a good start to a single project wide GlobalUsing.cs file.

If you want to remove this behavior and manually control all namespaces in your project, add <ImplicitUsings> disable </ImplicitUsings> in the project file. See MSDocs - Disable implicit using statements.

Create a new class file named GlobalUsing.cs in the root folder. Replace all code including the namespace with the global usings generated by ImplicitUsings.

GlobalUsings.cs:
global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Routing;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Net.Http.Json;
global using System.Threading;
global using System.Threading.Tasks;

As you migrate usings from code files to the GlobalUsing.cs file, you can implement file scoped namespaces. Remove the namespace's braces and add a semicolon to the end of the namespace declaration.

Before:
namespace WebApplication1.Pages
{
    // Code
}
After:
namespace WebApplication1.Pages;
// Code

The Remove and Sort Usings command on the right click context menu works on global usings. As you add usings to GlobalUsing.cs, you will find most usings can be removed.

Remove and Sort Global Usings
Remove and Sort Code File Usings

The UWD2FAP implements global usings for Microsoft. AspNetCore. Authentication and Microsoft. AspNetCore. WebUtilities. Both have a Base64UrlTextEncoder class which allows encoding and decoding base-64 url encoded text. This presents a Compiler Error CS0104 (Your program contains using directives for two namespaces and your code references a name that appears in both namespaces.) I mitigated the issue by declaring the Microsoft. AspNetCore. WebUtilities. Base64UrlTextEncoder as Base64UrlTextEncoder in GlobalUsings.cs.

global using Base64UrlTextEncoder = Microsoft.AspNetCore.WebUtilities.Base64UrlTextEncoder;
Notes:
After updating the namespace syntax, use the Ctrl+K, Ctrl+D keyboard shortcut to format a document and reduce the code indent.
Search for "using" and indented " public class" with Find in Current project to locate files which need migration.

Article Tags:

VS 2022

Comment Count = 0

Please log in to comment.

Login Register
Logged in users receive web notifications.
Web Notifications