更新丨.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
開始使用?
要開始使用 .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
升級現有項目
要將現有的 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
推斷來自服務的 API 控制器操作參數
當類型配置為服務時,API 控制器操作的參數綁定現在通過依賴注入綁定參數。這意味著不再需要將 [FromServices] 屬性顯式應用于參數。
Services.AddScoped<SomeCustomType>();[Route("[controller]")] [ApiController] public class MyController : ControllerBase {// Both actions will bound the SomeCustomType from the DI containerpublic ActionResult GetWithAttribute([FromServices]SomeCustomType service) => Ok();public ActionResult Get(SomeCustomType service) => Ok(); }您可以通過設置 DisableImplicitFromServicesParameters 來禁用該功能:
Services.Configure<ApiBehaviorOptions>(options => {options.DisableImplicitFromServicesParameters = true; })SignalR 集線器方法的依賴注入
SignalR 集線器方法現在支持通過依賴注入 (DI) 注入服務。
Services.AddScoped<SomeCustomType>();public class MyHub : Hub {// SomeCustomType comes from DI by default nowpublic 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 提供端點描述和摘要
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")]() => ...)在Minimal API 中綁定來自標頭和查詢字符串的數組和 StringValue
在此版本中,您現在可以將 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]}")自定義 cookie 同意值
您現在可以使用新的 CookiePolicyOptions.ConsentCookieValue 屬性指定用于跟蹤用戶是否同意 cookie 使用策略的值。
感謝@daviddesmet 貢獻了這項改進!
@daviddesmet
https://github.com/daviddesmet
請求有關 IIS 卷影復制的反饋
在 .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
總結
我們希望您喜歡 .NET 7 中的 ASP.NET Core 預覽版。通過在 GitHub 上提交問題,讓我們知道您對這些新改進的看法。
感謝您試用 ASP.NET Core!
提交問題
https://github.com/dotnet/aspnetcore/issues/new
? 獲取ASP.NET Core文檔新增內容??
總結
以上是生活随笔為你收集整理的更新丨.NET 7 预览版2 中的 ASP.NET Core的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开源作者去世后,代码谁来继承?
- 下一篇: 使用 C# 实现 URL 安全的 Bas