.NET 7 预览版2 现已推出,其中包括对ASP.NET Core 的许多重大改进。
以下是此预览版中新增内容的摘要:
有关为.NET 7 计划的ASP.NET Core 工作的更多详细信息,请参阅GitHub 上的.NET 7 的完整ASP.NET Core 路线图。
要开始使用.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
要将现有的ASP.NET Core 应用从.NET 7 Preview 1 升级到.NET 7 Preview 2:
另请参阅.NET 7 的ASP.NET Core 中的重大更改的完整列表。
当类型配置为服务时,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;})您可以通过设置DisableImplicitFromServicesParameters 来禁用该功能:
Services.Configure<ApiBehaviorOptions>(options =>{ options.DisableImplicitFromServicesParameters = true;})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);}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")]() => ...)
在此版本中,您现在可以将HTTPS 标头和查询字符串中的值绑定到原始类型数组、字符串数组或StringValues:
// Bind query string values to a primitive type array// GET /tags?q=1&q=2&q=3app.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=janeapp.MapGet("/tags", (string[] names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")// Bind to StringValues// GET /tags?names=john&names=jack&names=janeapp.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=3app.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=janeapp.MapGet("/tags", (string[] names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")// Bind to StringValues// GET /tags?names=john&names=jack&names=janeapp.MapGet("/tags", (StringValues names) => $"tag1: {names[0]} , tag2: {names[1]}, tag3: {names[2]}")您现在可以使用新的CookiePolicyOptions.ConsentCookieValue 属性指定用于跟踪用户是否同意cookie 使用策略的值。
感谢@daviddesmet贡献了这项改进!
在.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 上与我们分享您的反馈。
我们希望您喜欢.NET 7 中的ASP.NET Core 预览版。通过在GitHub上提交问题,让我们知道您对这些新改进的看法。
感谢您试用ASP.NET Core!