【问题标题】:How to generate cshtml class file in console app with dotnet cli?如何使用 dotnet cli 在控制台应用程序中生成 cshtml 类文件?
【发布时间】:2022-08-19 14:02:16
【问题描述】:

在 net 6.0 Sdk=\"Microsoft.NET.Sdk.Web\" 项目中有一个 cshtml 文件夹,

使用 vs for mac,我可以在 csproj 文件中添加跟随属性

    <ItemGroup>
      <None Update=\"Templates\\A.cshtml\">
        <Generator>RazorTemplatePreprocessor</Generator>
        <LastGenOutput>A.cs</LastGenOutput>
      </None>
      <None Update=\"Templates\\B.cshtml\">
        <Generator>RazorTemplatePreprocessor</Generator>
        <LastGenOutput>B.cs</LastGenOutput>
      </None>
    </ItemGroup>
    <ItemGroup>
      <Compile Update=\"Templates\\A.cs\">
        <DependentUpon>A.cshtml</DependentUpon>
      </Compile>
      <Compile Update=\"Templates\\B.cs\">
        <DependentUpon>B.cshtml</DependentUpon>
      </Compile>
    </ItemGroup>

并且 cs 文件将在 Templates 文件夹中自动生成。

但是在linux env中,没有vs for mac,那么如何将cshtml文件编译为cs文件

并将cs文件模板导入代码,然后生成html字符串发送到电子邮件?

    标签: c# .net asp.net-core razor


    【解决方案1】:

    毕竟,我用RazorTemplating 找到了一个更简单的解决方案

    添加这个包

        <PackageReference Include="Razor.Templating.Core" Version="1.7.1" />
    

    在代码中:

    // assume the Templates is a folder in the project directory first depth
    // and this usage code file is in Other first depth folder
    var emailContent = await RazorTemplateEngine.RenderAsync("Templates/SupervisorDigest.cshtml", htmlModelRecord);
    

    它可以正常工作,并且比 Visual Studio 预编译工具简单

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2010-10-09
      • 1970-01-01
      • 2017-08-17
      • 2019-07-10
      相关资源
      最近更新 更多