Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ASP.NET Core updates in .NET 5 Preview 8
1
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:
  • Azure Active Directory authentication with Microsoft.Identity.Web

  • CSS isolation for Blazor components

  • Lazy loading in Blazor WebAssembly

  • Updated Blazor WebAssembly globalization support

  • New
    Code:
    InputRadio
    Blazor component

  • Set UI focus in Blazor apps

  • Influencing the HTML head in Blazor apps

  • Code:
    IAsyncDisposable
    for Blazor components

  • 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:
h1
elements rendered by other components, for example, are not affected.

To write a selector in component specific styles that affects child components, use the
Code:
::deep
combinator.

Code:
.parent ::deep .child { color: red; }

By using the
Code:
::deep
combinator, only the
Code:
.parent
class selector is scoped to the component; the
Code:
.child
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
Code:
BlazorWebAssemblyLazyLoad
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
Code:
LazyAssemblyLoader
service:

Code:
@inject LazyAssemblyLoader LazyAssemblyLoader @code { var assemblies = await LazyAssemblyLoader.LoadAssembliesAsync(new string[] { "Lib1.dll" }); }

To lazy load assemblies for a specific page, use the
Code:
OnNavigateAsync
event on the
Code:
Router
component. The
Code:
OnNavigateAsync
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
Code:
Router
.

The following examples demonstrates using the
Code:
LazyAssemblyLoader
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
Code:
Router
component, so that it can discover any routable components in that assembly.

Code:
@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); } } }
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
Code:
InputRadio
Blazor component


Blazor in .NET 5 now includes built-in
Code:
InputRadio
and
Code:
InputRadioGroup
components. These components simplify data binding to radio button groups with integrated validation alongside the other Blazor form input components.

Code:
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>
Set UI focus in Blazor apps

Blazor now has a
Code:
FocusAsync
convenience method on
Code:
ElementReference
for setting the UI focus on that element.

Code:
<button @onclick="() => textInput.FocusAsync()">Set focus</button> <input @ref="textInput"/>
Code:
IAsyncDisposable
support for Blazor components


Blazor components now support the
Code:
IAsyncDisposable
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
Code:
IComponentActivator
service implementation.

Thank you Mladen Macanović for this Blazor feature contribution!

Influencing the HTML head in Blazor apps

Use the new
Code:
Title
,
Code:
Link
, and
Code:
Meta
components to programmatically set the title of a page and dynamically add
Code:
link
and
Code:
meta
tags to the HTML head in a Blazor app.

To use the new
Code:
Title
,
Code:
Link
, and
Code:
Meta
components:

  1. Add a package reference to the Microsoft.AspNetCore.Components.Web.Extensions package.

  2. Include a script reference to _content/Microsoft.AspNetCore.Components.Web.Extensions/headManager.js.

  3. Add a
    Code:
    @using
    directive for
    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" /> }
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:
Code:
ProtectedLocalStorage
and
Code:
ProtectedSessionStorage
. 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:

  1. Add a package reference to Microsoft.AspNetCore.Components.Web.Extensions.

  2. Configure the services by calling
    Code:
    services.AddProtectedBrowserStorage()
    from
    Code:
    Startup.ConfigureServcies
    .

  3. Inject either
    Code:
    ProtectedLocalStorage
    and
    Code:
    ProtectedSessionStorage
    into your component implementation:

    Code:
    @inject ProtectedLocalStorage ProtectedLocalStorage @inject ProtectedSessionStorage ProtectedSessionStorage

  4. 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:
PersonController
below uses the
Code:
Person
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) { // … } }

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" />
Improvements to
Code:
DynamicRouteValueTransformer


ASP.NET Core in .NET Core 3.1 introduced
Code:
DynamicRouteValueTransformer
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
Code:
DynamicRouteValueTransformer
and filter the set of endpoints chosen.

Auto refresh with
Code:
dotnet watch


In .NET 5, running
Code:
dotnet watch
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
Code:
dotnet watch
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
Code:
Microsoft.Extensions.Logging
library. Developers can now implement a custom
Code:
ConsoleFormatter
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
Code:
VT-100
(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
Code:
simple
logger to JSON, add to following snippet to your
Code:
Program.cs
:

Code:
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}" } }
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!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: