【译】ASP.NET Core在 .NET Core 3.1 Preview 1中的更新

<p>.NET Core 3.1 Preview 1如今可用。此版本主要侧重于错误修复,但同时也包含一些新功能。<br> 这是此版本的ASP.NET Core的新增功能:</p> <ul> <li>对Razor components的部分类支持</li> <li>将参数传递给顶级组件</li> <li>在HttpSysServer中支持共享队列</li> <li>在SameSite cookies的重大更改</li> </ul> <p>除了.NET Core 3.1 Preview版本发布以外,咱们还发布了Blazor WebAssembly的更新,如今要求.NET Core 3.1. 若要使用Blazor WebAssembly,您须要安装.NET Core 3.1 Preview 1以及Visual Studio的最新预览版。</p> <p>有关其余详细信息和已知问题,请参见<a href="https://github.com/dotnet/core/tree/master/release-notes/3.1">发行说明</a></p> <h2>开始吧</h2> <p>要在.NET Core 3.1 Preview 1 中使用ASP.NET Core,须要安装<a href="https://dotnet.microsoft.com/download/dotnet-core/3.1">.NET Core Preview 1 SDK</a>。</p> <p>若是你是在Windows上使用的Visual Studio,为得到最佳体验,建议你安装Visual Studio 2019 16.4 的最新预览版。安装Visual Studio 2019 16.4 还将安装上.NET Core 3.1 Preview 1,所以你无需单独安装它。为在.NET Core 3.1 中使用Blazor 开发,Visual Studio 2019 16.4是必须的。</p> <p>要安装最新的Blazor WebAssembly模板,请运行如下命令:</p> <p><code data-backticks="1">dotnet new -i Microsoft.AspNetCore.Blazor.Templates::3.1.0-preview1.19508.20</code></p> <h2>升级现有项目</h2> <p>要将现有的ASP.NET Core 3.0项目升级到3.1 Preview 1:</p> <ul> <li>将全部针对netcoreapp3.0的项目更新为netcoreapp3.1</li> <li>将全部Microsoft.AspNetCore.*软件包引用更新为3.1.0-preview1.19506.1</li> </ul> <p>另请参阅ASP.NET Core 3.1中<a href="https://github.com/aspnet/announcements/issues?utf8=%E2%9C%93&amp;q=is%3Aissue+label%3A3.1.0+label%3A%22Breaking+change%22">重大更改</a>的完成列表。</p> <p>如今,您应该都已准备好使用.NET Core 3.1 Preview 1!</p> <h2>对Razor components的部分类支持</h2> <p>Razor components如今做为分布类生成。你可使用定义为局部类的代码隐藏文件编写Razor components的代码,而不用在单个文件中定义该组件的全部代码。</p> <p>例如,不是用<code data-backticks="1">@code</code>块定义默认的Counter component,而是这样:<br> Counter.razor</p> <pre><code>@page "/counter"html

<h1>Counter</h1>git

<p>Current count: @currentCount</p>github

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>web

@code { int currentCount = 0;浏览器

void IncrementCount() { currentCount++; } } </code></pre>cookie

<p>如今,你可使用部分类将代码分离为代码隐藏文件:<br> Counter.razor</p> <pre><code>@page "/counter"app

<h1>Counter</h1>ui

<p>Current count: @currentCount</p>spa

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> </code></pre>code

<p>Counter.razor.cs</p> <pre><code>namespace BlazorApp1.Pages { public partial class Counter { int currentCount = 0;

void IncrementCount()
   {
       currentCount++;
   }

} } </code></pre>

<h2>将参数传递给顶级组件</h2> <p>如今,Blazor Server应用程序能够在初始渲染期间将参数传递给顶级组件(top-level components)。之前,你只能使用<code data-backticks="1">RenderMode.Static</code>将参数传递给顶级组件。在这次发布的版本中,同时支持<code data-backticks="1">RenderMode.Server</code>和<code data-backticks="1">RenderModel.ServerPrerendered</code>。任何指定的参数值都将序列化为JSON,并包含在初始响应中。</p> <p>例如,你可使用特定的当前计数来渲染Counter组件,以下所示</p> <pre><code>@(await Html.RenderComponentAsync&lt;Counter&gt;(RenderMode.ServerPrerendered, new { CurrentCount = 123 })) </code></pre> <h2>在HttpSysServer中支持共享队列</h2> <p>除了HttpSysServer建立匿名请求队列的现有行为外,咱们还添加了建立或附加到现有命名HTTP.sys 请求队列的功能。<br> 这应该启用一下方案:拥有队列的HTTP.Sys控制器进程独立于侦听器进程,从而能够在跨多个侦听器进程从新启动之间保留现有的链接和排队的请求。</p> <pre><code>public static IHostBuilder CreateHostBuilder(string[] args) =&gt; Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder =&gt; { // ... webBuilder.UseHttpSys(options =&gt; { options.RequestQueueName = "MyExistingQueue", options.RequestQueueMode = RequestQueueMode.CreateOrAttach }) }); </code></pre> <h2>在SameSite cookies的重大更改</h2> <p>此版本更新了ASP.NET Core中SameSite cookie的行为,以符合浏览器强制执行的最新标准。有关这些更改及其对现有应用程序的影响的详细信息,请参见https://github.com/aspnet/Announcements/issues/390。</p> <h2>给予反馈</h2> <p>咱们但愿您喜欢此ASP.NET Core预览版中的新功能!经过在<a href="https://github.com/aspnet/aspnetcore/issues">GitHub</a>上提交问题,请让咱们知道您的想法。</p> <p>感谢您试用ASP.NET Core!</p>

原文出处:https://www.cnblogs.com/sesametech-netcore/p/11723840.html