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

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

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

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

开始使用

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

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

要安装最新的 .NET 构建工具,请从提升的命令提示符处运行以下命令:

wasm工具

升级现有项目

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

另请参阅 ASP.NET Core for .NET 7 中的完整列表。

从服务推断 API 控制器操作参数

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

Services.AddScoped();
[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(options =>
{
     options.DisableImplicitFromServicesParameters = true;
})

您可以通过设置禁用此功能:

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

hub方法的依赖注入

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

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

您可以通过设置禁用此功能:

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

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

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

提供 API 的端点描述和摘要

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

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

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

app.(“/hello”, [(“发送一个 Hello 到 “)]() => …)

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

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

// 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]}")

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

// 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]}")

自定义同意值

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

感谢@@为改进做出贡献!

© 版权声明
THE END
喜欢就支持一下吧
点赞15赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容