使用 GitHub Actions 将 .Net Web API 部署到 Azure 应用服务,并包含电子邮件模板文件

Deploy .Net Web API to Azure App Service using GitHub Actions and include email template file

提问人:Jatin Chhabra 提问时间:11/17/2023 更新时间:11/17/2023 访问量:45

问:

我有一个 .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

C# .NET Azure-Web-App-Service GitHub 操作

评论

0赞 Thiago Custodio 11/17/2023
您应该使用虚拟路径访问它。仅尝试电子邮件模板\Otp验证.htm

答:

0赞 SiddheshDesai 11/17/2023 #1

感谢 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如下所示:-

我的文件夹:-

enter image description here

我的程序.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();

本地输出:-

enter image description here

我将此代码添加到我的 Github 存储库中,并使用下面的 github 操作工作流来构建并在 Azure Web 应用中部署整个 dotnet api:-

在 Visual Studio 中打开解决方案,并通过 Github Actions 工作流 CI/CD 发布它,如下所示:-

enter image description here

Github Action 工作流已成功创建并运行,如下所示:-

enter image description here

评论

0赞 Jatin Chhabra 11/18/2023
谢谢你的解决方案,我不得不再问一件事,在csproj文件中,你特别包括OtpValidation文件,将来如果我包含多个模板,我需要手动添加它们,或者我可以简单地包括EmailTemplates文件夹,它会自动包含(因为我包括父文件夹本身)?
0赞 SiddheshDesai 11/18/2023
很高兴它有所帮助,如果模板位于父文件夹中,则它们将一起构建,并且您无需在 .csproj 文件中添加参数,但如果任何模板无法构建,您可以手动在 .csproj 中引用该模板。因为它默认不添加。