08-27-2020, 06:44 AM
ASP.NET Core updates in .NET 5 Preview 8
Daniel
August 25th, 2020
.NET 5 Preview 8 is now available and is ready for evaluation. Here’s what’s new in this release:
See the .NET 5 release notes for additional details and known issues.
Get started
To get started with ASP.NET Core in .NET 5 Preview 8 install the .NET 5 SDK.
You need to use Visual Studio 2019 16.8 Preview 2 or newer to use .NET 5 Preview 8. .NET 5 is also supported with the latest preview of Visual Studio for Mac. To use .NET 5 with Visual Studio Code, install the latest version of the C# extension.
Upgrade an existing project
To upgrade an existing ASP.NET Core app from .NET 5 Preview 7 to .NET 5 Preview 8:
That’s it! You should be all ready to go.
See also the full list of breaking changes in ASP.NET Core for .NET 5.
What’s new?
Azure Active Directory authentication with Microsoft.Identity.Web
The ASP.NET Core project templates now integrate with Microsoft.Identity.Web to handle authentication with Azure Activity Directory (Azure AD). The Microsoft.Identity.Web package provides a better experience for authentication through Azure AD as well as an easier way to access Azure resources on behalf of your users, including Microsoft Graph. Check out the Microsoft.Identity.Web sample that take you from a simple login through multi-tenancy, using Azure APIs, using Microsoft Graph, and protecting your own APIs. Microsoft.Identity.Web will be generally available alongside .NET 5.
CSS isolation for Blazor components
Blazor now supports defining CSS styles that are scoped to a given component. Component specific CSS styles make it easier to reason about the styles in your app and to avoid unintentional side effects from global styles. You define component specific styles in a .razor.css file the matches the name of the .razor file for the component.
For example, let’s say you have a component MyComponent.razor file that looks like this:
MyComponent.razor
You can then define a MyComponent.razor.css with the styles for MyComponent:
MyComponent.razor.css
The styles in MyComponent.razor.css will only get applied to the rendered output of MyComponent; the elements rendered by other components, for example, are not affected.
To write a selector in component specific styles that affects child components, use the combinator.
By using the combinator, only the class selector is scoped to the component; the class selector is not scoped, and will match content from child components.
Blazor achieves CSS isolation by rewriting the CSS selectors as part of the build so that they only match markup rendered by the component. Blazor then bundles together the rewritten CSS files and makes the bundle available to the app as a static web asset at the path _framework/scoped.styles.css.
While Blazor doesn’t natively support CSS preprocessors like Sass or Less, you can still use CSS preprocessors to generate component specific styles before they are rewritten as part of the building the project.
Lazy loading in Blazor WebAssembly
Lazy loading enables you to improve the load time of your Blazor WebAssembly app by deferring the download of specific app dependencies until they are required. Lazy loading may be helpful if your Blazor WebAssembly app has large dependencies that are only used for specific parts of the app.
To delay the loading of an assembly, you add it to the item group in your project file:
Assemblies marked for lazy loading must be explicitly loaded by the app before they are used. To lazy load assemblies at runtime, use the service:
To lazy load assemblies for a specific page, use the event on the component. The event is fired on every page navigation and can be used to lazy load assemblies for a particular route. You can also lazily load the entire page for a route by passing any lazy loaded assemblies as additional assemblies to the .
The following examples demonstrates using the service to lazy load a specific dependency (Lib1.dll) when the user navigates to /page1. The lazy loaded assembly is then added to the additional assemblies list passed to the component, so that it can discover any routable components in that assembly.
Updated Blazor WebAssembly globalization support
.NET 5 Preview 8 reintroduces globalization support for Blazor WebAssembly based on International Components for Unicode (ICU). Part of introducing the ICU data and logic is optimizing these payloads for download size. This work is not fully completed yet. We expect to reduce the size of the ICU data in future .NET 5 preview updates.
New Blazor component
Blazor in .NET 5 now includes built-in and components. These components simplify data binding to radio button groups with integrated validation alongside the other Blazor form input components.
Set UI focus in Blazor apps
Blazor now has a convenience method on for setting the UI focus on that element.
support for Blazor components
Blazor components now support the interface for the asynchronous release of allocated resources.
Control Blazor component instantiation
You can now control how Blazor components are instantiated by providing your own service implementation.
Thank you Mladen Macanović for this Blazor feature contribution!
Influencing the HTML head in Blazor apps
Use the new, , and components to programmatically set the title of a page and dynamically add and tags to the HTML head in a Blazor app.
To use the new, , and components:
The following example programmatically sets the page title to show the number of unread user notifications, and updates the page icon a as well:
Protected browser storage
In Blazor Server apps, you may want to persist app state in local or session storage so that the app can rehydrate it later if needed. When storing app state in the user’s browser, you also need to ensure that it hasn’t been tampered with.
Blazor in .NET 5 helps solve this problem by providing two new services: and . These services help you store state in local and session storage respectively, and they take care of protecting the stored data using the ASP.NET Core data protection APIs.
To use the new protected browser storage services:
Model binding and validation with C# 9 record types
You can now use C# 9 record types with model binding in an MVC controller or a Razor Page. Record types are a great way to model data being transmitted over the wire.
For example, the below uses the record type with model binding and form validation:
“`C# public record Person([Required] string Name, [Range(0, 150)] int Age);
public class PersonController { public IActionResult Index() => View();
[HttpPost] public IActionResult Index(Person person) { // … } }
Improvements to
ASP.NET Core in .NET Core 3.1 introduced as a way to use use a custom endpoint to dynamically select an MVC controller action or a razor page. In .NET 5 Preview 8 you can now pass state to your and filter the set of endpoints chosen.
Auto refresh with
In .NET 5, running on an ASP.NET Core project will now both launch the default browser and auto refresh the browser as you make changes to your code. This means you can open an ASP.NET Core project in your favorite text editor, run run once, and then focus on your code changes while the tooling handles rebuilding, restarting, and reloading your app. We expect to bring the auto refresh functionality to Visual Studio in the future as well.
Console Logger Formatter
We’ve made improvements to the console log provider in the library. Developers can now implement a custom to exercise complete control over formatting and colorization of the console output. The formatter APIs allow for rich formatting by implementing a subset of the (supported by most modern terminals) escape sequences. The console logger can parse out escape sequences on unsupported terminals allowing you to author a single formatter for all terminals.
JSON Console Logger
In addition to support for custom formatters, we’ve also added a built-in JSON formatter that emits structured JSON logs to the console. You can switch from the default logger to JSON, add to following snippet to your :
Once enabled, log messages emitted to the console are now JSON formatted.
Give feedback
We hope you enjoy this release of ASP.NET Core in .NET 5! We are eager to hear about your experiences with this latest .NET 5 release. Let us know what you think by filing issues on GitHub.
Thanks for trying out ASP.NET Core!
Daniel
August 25th, 2020
.NET 5 Preview 8 is now available and is ready for evaluation. Here’s what’s new in this release:
- Azure Active Directory authentication with Microsoft.Identity.Web
- CSS isolation for Blazor components
- Lazy loading in Blazor WebAssembly
- Updated Blazor WebAssembly globalization support
- New Blazor componentCode:
InputRadio
- Set UI focus in Blazor apps
- Influencing the HTML head in Blazor apps
- for Blazor componentsCode:
IAsyncDisposable
- Control Blazor component instantiation
- Protected browser storage
- Model binding and validation with C# 9 record types
- Improvements to Code:
DynamicRouteValueTransformer
- Auto refresh with Code:
dotnet watch
- Console Logger Formatter
- JSON Console Logger
See the .NET 5 release notes for additional details and known issues.
Get started
To get started with ASP.NET Core in .NET 5 Preview 8 install the .NET 5 SDK.
You need to use Visual Studio 2019 16.8 Preview 2 or newer to use .NET 5 Preview 8. .NET 5 is also supported with the latest preview of Visual Studio for Mac. To use .NET 5 with Visual Studio Code, install the latest version of the C# extension.
Upgrade an existing project
To upgrade an existing ASP.NET Core app from .NET 5 Preview 7 to .NET 5 Preview 8:
- Update all Microsoft.AspNetCore.* package references to .Code:
5.0.0-preview.8.*
- Update all Microsoft.Extensions.* package references to .Code:
5.0.0-preview.8.*
- Update System.Net.Http.Json package references to .Code:
5.0.0-preview.8.*
That’s it! You should be all ready to go.
See also the full list of breaking changes in ASP.NET Core for .NET 5.
What’s new?
Azure Active Directory authentication with Microsoft.Identity.Web
The ASP.NET Core project templates now integrate with Microsoft.Identity.Web to handle authentication with Azure Activity Directory (Azure AD). The Microsoft.Identity.Web package provides a better experience for authentication through Azure AD as well as an easier way to access Azure resources on behalf of your users, including Microsoft Graph. Check out the Microsoft.Identity.Web sample that take you from a simple login through multi-tenancy, using Azure APIs, using Microsoft Graph, and protecting your own APIs. Microsoft.Identity.Web will be generally available alongside .NET 5.
CSS isolation for Blazor components
Blazor now supports defining CSS styles that are scoped to a given component. Component specific CSS styles make it easier to reason about the styles in your app and to avoid unintentional side effects from global styles. You define component specific styles in a .razor.css file the matches the name of the .razor file for the component.
For example, let’s say you have a component MyComponent.razor file that looks like this:
MyComponent.razor
Code:
<h1>My Component</h1> <ul class="cool-list"> <li>Item1</li> <li>Item2</li>
</ul>You can then define a MyComponent.razor.css with the styles for MyComponent:
MyComponent.razor.css
Code:
h1 { font-family: 'Comic Sans MS'
} .cool-list li { color: red;
}The styles in MyComponent.razor.css will only get applied to the rendered output of MyComponent; the
Code:
h1To write a selector in component specific styles that affects child components, use the
Code:
::deepCode:
.parent ::deep .child { color: red;
}By using the
Code:
::deepCode:
.parentCode:
.childBlazor achieves CSS isolation by rewriting the CSS selectors as part of the build so that they only match markup rendered by the component. Blazor then bundles together the rewritten CSS files and makes the bundle available to the app as a static web asset at the path _framework/scoped.styles.css.
While Blazor doesn’t natively support CSS preprocessors like Sass or Less, you can still use CSS preprocessors to generate component specific styles before they are rewritten as part of the building the project.
Lazy loading in Blazor WebAssembly
Lazy loading enables you to improve the load time of your Blazor WebAssembly app by deferring the download of specific app dependencies until they are required. Lazy loading may be helpful if your Blazor WebAssembly app has large dependencies that are only used for specific parts of the app.
To delay the loading of an assembly, you add it to the
Code:
BlazorWebAssemblyLazyLoadAssemblies marked for lazy loading must be explicitly loaded by the app before they are used. To lazy load assemblies at runtime, use the
Code:
LazyAssemblyLoaderCode:
@inject LazyAssemblyLoader LazyAssemblyLoader @code { var assemblies = await LazyAssemblyLoader.LoadAssembliesAsync(new string[] { "Lib1.dll" });
}To lazy load assemblies for a specific page, use the
Code:
OnNavigateAsyncCode:
RouterCode:
OnNavigateAsyncCode:
RouterThe following examples demonstrates using the
Code:
LazyAssemblyLoaderCode:
RouterCode:
@using System.Reflection
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.WebAssembly.Services
@inject LazyAssemblyLoader LazyAssemblyLoader <Router AppAssembly="typeof(Program).Assembly" AdditionalAssemblies="lazyLoadedAssemblies" OnNavigateAsync="@OnNavigateAsync"> <Navigating> <div> <p>Loading the requested page...</p> </div> </Navigating> <Found Context="routeData"> <RouteView RouteData="@routeData" DefaultLayout="typeof(MainLayout)" /> </Found> <NotFound> <LayoutView Layout="typeof(MainLayout)"> <p>Sorry, there is nothing at this address.</p> </LayoutView> </NotFound>
</Router> @code { private List<Assembly> lazyLoadedAssemblies = new List<Assembly>(); private async Task OnNavigateAsync(NavigationContext args) { if (args.Path.EndsWith("/page1")) { var assemblies = await LazyAssemblyLoader.LoadAssembliesAsync(new string[] { "Lib1.dll" }); lazyLoadedAssemblies.AddRange(assemblies); } }
}.NET 5 Preview 8 reintroduces globalization support for Blazor WebAssembly based on International Components for Unicode (ICU). Part of introducing the ICU data and logic is optimizing these payloads for download size. This work is not fully completed yet. We expect to reduce the size of the ICU data in future .NET 5 preview updates.
New
Code:
InputRadioBlazor in .NET 5 now includes built-in
Code:
InputRadioCode:
InputRadioGroupCode:
Opinion about blazor:
<InputRadioGroup @bind-Value="survey.OpinionAboutBlazor"> @foreach (var opinion in opinions) { <div class="form-check"> <InputRadio class="form-check-input" id="@opinion.id" Value="@opinion.id" /> <label class="form-check-label" for="@opinion.id">@opinion.label</label> </div> }
</InputRadioGroup>Blazor now has a
Code:
FocusAsyncCode:
ElementReferenceCode:
<button @onclick="() => textInput.FocusAsync()">Set focus</button>
<input @ref="textInput"/>Code:
IAsyncDisposableBlazor components now support the
Code:
IAsyncDisposableControl Blazor component instantiation
You can now control how Blazor components are instantiated by providing your own
Code:
IComponentActivatorThank you Mladen Macanović for this Blazor feature contribution!
Influencing the HTML head in Blazor apps
Use the new
Code:
TitleCode:
LinkCode:
MetaCode:
linkCode:
metaTo use the new
Code:
TitleCode:
LinkCode:
Meta- Add a package reference to the Microsoft.AspNetCore.Components.Web.Extensions package.
- Include a script reference to _content/Microsoft.AspNetCore.Components.Web.Extensions/headManager.js.
- Add a directive forCode:
@using.Code:Microsoft.AspNetCore.Components.Web.Extensions.Head
The following example programmatically sets the page title to show the number of unread user notifications, and updates the page icon a as well:
Code:
@if (unreadNotificationsCount > 0)
{ var title = $"Notifications ({unreadNotificationsCount})"; <Title Value="title"></Title> <Link rel="icon" href="icon-unread.ico" />
}In Blazor Server apps, you may want to persist app state in local or session storage so that the app can rehydrate it later if needed. When storing app state in the user’s browser, you also need to ensure that it hasn’t been tampered with.
Blazor in .NET 5 helps solve this problem by providing two new services:
Code:
ProtectedLocalStorageCode:
ProtectedSessionStorageTo use the new protected browser storage services:
- Add a package reference to Microsoft.AspNetCore.Components.Web.Extensions.
- Configure the services by calling fromCode:
services.AddProtectedBrowserStorage().Code:Startup.ConfigureServcies
- Inject either andCode:
ProtectedLocalStorageinto your component implementation:Code:ProtectedSessionStorage
Code:@inject ProtectedLocalStorage ProtectedLocalStorage @inject ProtectedSessionStorage ProtectedSessionStorage
- Use the desired service to get, set, and delete state asynchronously:
Code:private async Task IncrementCount() { await ProtectedLocalStorage.SetAsync("count", ++currentCount); }
Model binding and validation with C# 9 record types
You can now use C# 9 record types with model binding in an MVC controller or a Razor Page. Record types are a great way to model data being transmitted over the wire.
For example, the
Code:
PersonControllerCode:
Person“`C# public record Person([Required] string Name, [Range(0, 150)] int Age);
public class PersonController { public IActionResult Index() => View();
[HttpPost] public IActionResult Index(Person person) { // … } }
Code:
<br />*Person/Index.cshtml* ```razor
@model Person Name: <input asp-for="Model.Name" />
<span asp-validation-for="Model.Name" /> Age: <input asp-for="Model.Age" />
<span asp-validation-for="Model.Age" />Code:
DynamicRouteValueTransformerASP.NET Core in .NET Core 3.1 introduced
Code:
DynamicRouteValueTransformerCode:
DynamicRouteValueTransformerAuto refresh with
Code:
dotnet watchIn .NET 5, running
Code:
dotnet watchCode:
dotnet watchConsole Logger Formatter
We’ve made improvements to the console log provider in the
Code:
Microsoft.Extensions.LoggingCode:
ConsoleFormatterCode:
VT-100JSON Console Logger
In addition to support for custom formatters, we’ve also added a built-in JSON formatter that emits structured JSON logs to the console. You can switch from the default
Code:
simpleCode:
Program.csCode:
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
+ .ConfigureLogging(logging =>
+ {
+ logging.AddJsonConsole(options =>
+ {
+ options.JsonWriterOptions = new JsonWriterOptions() { Indented = true };
+ });
+ }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });Once enabled, log messages emitted to the console are now JSON formatted.
Code:
{ "EventId": 0, "LogLevel": "Information", "Category": "Microsoft.Hosting.Lifetime", "Message": "Now listening on: https://localhost:5001", "State": { "Message": "Now listening on: https://localhost:5001", "address": "https://localhost:5001", "{OriginalFormat}": "Now listening on: {address}" }
}We hope you enjoy this release of ASP.NET Core in .NET 5! We are eager to hear about your experiences with this latest .NET 5 release. Let us know what you think by filing issues on GitHub.
Thanks for trying out ASP.NET Core!

