共用方式為


.NET Aspire Azure 靜態 Web Apps 模擬器整合

包括:包含託管整合 僅限託管整合 — Client不包含整合Client不包含整合

備註

這項整合是 .NET.NET Aspire 社群工具組 的一部分, 不受 .NET.NET Aspire 小組正式支援。

在本文中,您將瞭解如何使用 .NET.NET AspireAzure 靜態 Web Apps 模擬器 裝載整合,使用模擬器在本機執行 Azure Static Web Apps。 模擬器支援使用應用程式主機中定義的資源來 Proxy 處理靜態前端和 API 後端。

此整合功能需要 Azure 靜態 Web Apps CLI 才能執行,並且只支援啟動模擬器以進行本機開發,而非部署到 Azure 靜態 Web Apps。

主機托管整合

若要開始使用 .NET AspireAzure 靜態 Web Apps 模擬器裝載整合,請在 AppHost 專案中安裝 📦 CommunityToolkit.AspireHosting.AzureStaticWebApps NuGet 套件。

dotnet add package CommunityToolkit.Aspire.Hosting.Azure.StaticWebApps

如需詳細資訊,請參閱 dotnet add package管理 .NET 應用程式中的套件相依性

範例使用方式

Program.cs 應用程式主機項目的檔案中,定義後端和前端資源。 然後,呼叫 AddSwaEmulator 方法來建立模擬器,並使用 WithAppResourceWithApiResource 方法傳遞資源。

var builder = DistributedApplication.CreateBuilder(args);

// Define the API resource
var api = builder.AddProject<Projects.Aspire_CommunityToolkit_StaticWebApps_ApiApp>("api");

// Define the frontend resource
var web = builder
    .AddNpmApp("web", Path.Combine("..", "CommunityToolkit.Aspire.StaticWebApps.WebApp"), "dev")
    .WithHttpEndpoint(env: "PORT")
    .WithExternalHttpEndpoints();

// Create a SWA emulator with the frontend and API resources
_ = builder
    .AddSwaEmulator("swa")
    .WithAppResource(web)
    .WithApiResource(api);

builder.Build().Run();

上述程式代碼會定義 API 和前端資源,並使用資源建立模擬器。 然後,模擬器會開始使用 Run 方法。

另請參閱