更新丨.NET 7 预览版2 中的 ASP.NET Core

点击上方蓝字 

关注我们

(本文阅读时间:6分钟)

.NET 7 预览版2 现已推出,其中包括对 ASP.NET Core 的许多重大改进。

以下是此预览版中新增内容的摘要:

• 推断来自服务的 API 控制器操作参数;

• SignalR 集线器方法的依赖注入;

• 为minimal API 提供端点描述和摘要;

• 在最小的 API 中绑定来自标头和查询字符串的数组和 StringValue;

• 自定义 cookie 同意值。

有关为 .NET 7 计划的 ASP.NET Core 工作的更多详细信息,请参阅 GitHub 上的 .NET 7 的完整 ASP.NET Core 路线图。

  • .NET 7 预览版2

    https://devblogs.microsoft.com/dotnet/announcing-dotnet-7-preview-2?ocid=AID3042760

  • 完整 ASP.NET Core 路线图

    https://aka.ms/aspnet/roadmap?ocid=AID3042760

3674a13a79a8d9978b98ca4f5daa71da.png

开始使用 

2ec0445249239d9457ccc77cb38cc53c.png

要开始使用 .NET 7 Preview 2 中的 ASP.NET Core,请安装 .NET 7 SDK。

如果您在 Windows 上使用 Visual Studio,我们建议安装最新的 Visual Studio 2022 预览版。Visual Studio for Mac 对 .NET 7 预览的支持尚不可用,但即将推出。

要安装最新的 .NET WebAssembly 构建工具,请从提升的命令提示符处运行以下命令:dotnet workload install wasm-tools。

  •  .NET 7 SDK:

    https://dotnet.microsoft.com/download/dotnet/7.0?ocid=AID3042760

  • Visual Studio 2022 预览版:

    https://visualstudio.com/preview?ocid=AID3042760

26305463116e3263d02a3a63cc6f93ca.png

升级现有项目

9ca1eee9be4bbb25e796a8ea7b78b173.png

要将现有的 ASP.NET Core 应用从 .NET 7 Preview 1 升级到 .NET 7 Preview 2:

  • 将所有 Microsoft.AspNetCore.* 包引用更新到 7.0.0-preview.2.*。

  • 将所有 Microsoft.Extensions.* 包引用更新到 7.0.0-preview.2.*。

另请参阅 .NET 7 的 ASP.NET Core 中的重大更改的完整列表。

  • 重大更改完整列表

    https://docs.microsoft.com/dotnet/core/compatibility/7.0#aspnet-core?ocid=AID3042760

5817ca88c807d780683fd38acd2bbb79.png

推断来自服务的 API 控制器操作参数

ee23f338a6c1f9debf3fac270632e34d.png

当类型配置为服务时,API 控制器操作的参数绑定现在通过依赖注入绑定参数。这意味着不再需要将 [FromServices] 属性显式应用于参数。

Services.AddScoped<SomeCustomType>();


[Route("[controller]")]
[ApiController]
public class MyController : ControllerBase
{
    // Both actions will bound the SomeCustomType from the DI container
    public ActionResult GetWithAttribute([FromServices]SomeCustomType service) => Ok();
    public ActionResult Get(SomeCustomType service) => Ok();
}

您可以通过设置 DisableImplicitFromServicesParameters 来禁用该功能:

Services.Configure<ApiBehaviorOptions>(options =>
{
     options.DisableImplicitFromServicesParameters = true;
})

ed5ed0f75d522ebe164eb651535726d2.png

SignalR 集线器方法的依赖注入

64ac88d101e03d30609ef38e4bf5c7e2.png

SignalR 集线器方法现在支持通过依赖注入 (DI) 注入服务。

Services.AddScoped<SomeCustomType>();


public class MyHub : Hub
{
    // SomeCustomType comes from DI by default now
    public Task Method(string text, SomeCustomType type) => Task.CompletedTask;
}

您可以通过设置 DisableImplicitFromServicesParameters 来禁用该功能:

services.AddSignalR(options =>
{
    options.DisableImplicitFromServicesParameters = true;
});

要显式标记要从配置的服务绑定的参数,请使用 [FromServices] 属性:

public class MyHub : Hub
{
    public Task Method(string arguments, [FromServices] SomeCustomType type);
}

26720f1adf18fb44e07b243f566cdaa6.png

为Minimal API 提供端点描述和摘要

e1f3e152820f170ba247de69a2c2d546.png

Minimal API 现在支持使用用于 OpenAPI 规范生成的描述和摘要来注释操作。您可以使用扩展方法在Minimal API 应用程序中为路由处理程序设置这些描述和摘要:

app.MapGet("/hello", () => ...)
  .WithDescription("Sends a request to the backend HelloService to process a greeting request.");

或者通过路由处理程序委托上的属性设置描述或摘要:

app.MapGet("/hello", [EndpointSummary("Sends a Hello request to the backend")]() => ...)

a74c8e7220be9419b0b60cf2d192bf1d.png

在Minimal API 中绑定来自标头和查询字符串的数组和 StringValue

0afeb7efb27add43fa51f72c3c0d6363.png

在此版本中,您现在可以将 HTTPS 标头和查询字符串中的值绑定到原始类型数组、字符串数组或 StringValues:

// Bind query string values to a primitive type array
// GET  /tags?q=1&q=2&q=3
app.MapGet("/tags", (int[] q) => $"tag1: {q[0]} , tag2: {q[1]}, tag3: {q[2]}")


// Bind to a string array
// GET /tags?names=john&names=jack&names=jane
app.MapGet("/tags", (string[] names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")


// Bind to StringValues
// GET /tags?names=john&names=jack&names=jane
app.MapGet("/tags", (StringValues names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")

您还可以将查询字符串或标头值绑定到复杂类型的数组,只要该类型具有 TryParse 实现,如下例所示。

// Bind query string values to a primitive type array
// GET  /tags?q=1&q=2&q=3
app.MapGet("/tags", (int[] q) => $"tag1: {q[0]} , tag2: {q[1]}, tag3: {q[2]}")


// Bind to a string array
// GET /tags?names=john&names=jack&names=jane
app.MapGet("/tags", (string[] names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")


// Bind to StringValues
// GET /tags?names=john&names=jack&names=jane
app.MapGet("/tags", (StringValues names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")

26c64647c5a24bbc9ba8df3d3afe6baa.png

自定义 cookie 同意值

d457928b260840a81fb5fdbe6d9d546f.png

您现在可以使用新的 CookiePolicyOptions.ConsentCookieValue 属性指定用于跟踪用户是否同意 cookie 使用策略的值。

感谢@daviddesmet 贡献了这项改进!

  • @daviddesmet

    https://github.com/daviddesmet

f5483586a1c158bbccb82870cf525a01.png

请求有关 IIS 卷影复制的反馈

a091b5c31b2c445bc23215bfcb555f8a.png

在 .NET 6 中,我们为 IIS 的 ASP.NET Core 模块 (ANCM) 添加了对影子复制应用程序程序集的实验性支持。当 ASP.NET Core 应用程序在 Windows 上运行时,二进制文件被锁定,因此无法修改或替换它们。您可以通过部署应用程序离线文件来停止应用程序,但有时这样做不方便或不可能。卷影复制允许在应用程序运行时通过复制程序集来更新应用程序程序集。 

您可以通过在 web.config 中自定义 ANCM 处理程序设置来启用卷影复制:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <remove name="aspNetCore"/>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".logsstdout">
      <handlerSettings>
        <handlerSetting name="experimentalEnableShadowCopy" value="true" />
        <handlerSetting name="shadowCopyDirectory" value="../ShadowCopyDirectory/" />
      </handlerSettings>
    </aspNetCore>
  </system.webServer>
</configuration>

我们正在研究使 IIS 中的卷影复制成为 .NET 7 中 ASP.NET Core 的一项功能,并且我们正在寻求有关该功能是否满足用户要求的更多反馈。如果您将 ASP.NET Core 部署到 IIS,请尝试使用卷影复制并在 GitHub 上与我们分享您的反馈。

  • 应用程序离线文件

    https://docs.microsoft.com/aspnet/core/host-and-deploy/app-offline?ocid=AID3042760

  • 分享反馈

    https://github.com/dotnet/AspNetCore.Docs/issues/23733

1feb0d5903be68cf41dc91accf667ac1.png

总结

ddb517111a92a8b2fe0264f3a481759c.png

我们希望您喜欢 .NET 7 中的 ASP.NET Core 预览版。通过在 GitHub 上提交问题,让我们知道您对这些新改进的看法。

感谢您试用 ASP.NET Core!

提交问题

https://github.com/dotnet/aspnetcore/issues/new

79e74b5d3bab50254a64c5c80ba7c5d9.gif

  获取ASP.NET Core文档新增内容  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值