![]() |
|
ASP.NET Core updates in .NET 5 Preview 8 - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: C#, Visual Basic & .NET (https://sickgaming.net/forum-79.html) +--- Thread: ASP.NET Core updates in .NET 5 Preview 8 (/thread-96914.html) |
ASP.NET Core updates in .NET 5 Preview 8 - xSicKxBot - 08-27-2020 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 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
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:
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! |