提问人:Jatin Chhabra 提问时间:11/17/2023 更新时间:11/17/2023 访问量:45
使用 GitHub Actions 将 .Net Web API 部署到 Azure 应用服务,并包含电子邮件模板文件
Deploy .Net Web API to Azure App Service using GitHub Actions and include email template file
问:
我有一个 .Net Web API 项目,我已使用 Github 操作为其配置了 CI/CD,它正在部署在 Azure 应用服务上。最近,我设置了一个使用 .htm 模板发送电子邮件的终结点,由于此模板无法在已部署的 API 中解析,因此此终结点会引发错误 -
我在应用服务中检查了 scm,但找不到该模板。我不确定在通过 GitHub Actions 部署时如何包含特定文件。Could not find a part of the path C:\\home\\site\\wwwroot\\EmailTemplates\\OtpValidation.htm'.
存储库 - https://github.com/Jatinchhabra21/bookkeeper-api
已部署的 swagger - https://bookkeeper.azurewebsites.net/swagger/index.html
API 端点 - /api/users/new/otp
答:
感谢 Custodio @Thiago的评论。
在 .csproj
文件中,添加以下代码,将 EmailTemplates 文件夹始终复制到输出目录。
.csproj file:-
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.2.2" />
<PackageReference Include="Azure.Identity" Version="1.10.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.24" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.22" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<Content Include="EmailTemplates\OtpValidation.htm">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>
<Folder Include="Utility\" />
</ItemGroup>
</Project>
此外,在您的计划中.cs请确保通过添加>应用来启用静态投放。UseStaticFiles
选项如下:-
我使用 .Net 7.0 运行时
创建了一个 Asp.NET Core API
,并通过添加 OtpValidation.htm 和您的代码创建了一个带有 EmailTemplates 的文件夹,然后我编辑了我的程序.cs以提供 OtpValidation.htm如下所示:-
我的文件夹:-
我的程序.cs:-
using Microsoft.Extensions.FileProviders;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
// Configure static file serving from the EmailTemplates folder
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "EmailTemplates")), // Adjust the path as needed
RequestPath = "/EmailTemplates" // Virtual path to access files
});
app.UseAuthorization();
app.MapControllers();
app.Run();
本地输出:-
我将此代码添加到我的 Github 存储库中,并使用下面的 github 操作工作流来构建并在 Azure Web 应用中部署整个 dotnet api:-
在 Visual Studio 中打开解决方案,并通过 Github Actions 工作流 CI/CD 发布它,如下所示:-
Github Action 工作流已成功创建并运行,如下所示:-
评论