如何在Azure函数中添加Razor View File



我正在创建一个应用程序,该应用程序是Azure功能项目,我想在该项目中使用Razor View,我应该在Azure功能中使用任何模板引擎吗?

多亏了Razorlight Project和Azure函数中的一些进步,现在可以选择Razor。

警告在此示例中,视图在运行时进行了编译。如果您的Azure功能停止并经常开始。

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Reflection;
using RazorLight;
using System.Linq;
namespace RootNamespace
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            //check which view files have been embedded
            var names = executingAssembly.GetManifestResourceNames();
            names.ToList().ForEach(x => Console.WriteLine(x));
            var engine = new RazorLightEngineBuilder()
              .UseEmbeddedResourcesProject(executingAssembly, "RootNamespace.Views") //'Views' is folder with views 
              .SetOperatingAssembly(executingAssembly)
              .UseMemoryCachingProvider()
              .Build();
            string result = await engine.CompileRenderAsync<object>("View1.cshtml", "foobar model string");
            return new OkObjectResult(result);
        }
    }
}

.ViewsView1.cshtml

@model String;
@{
    Layout = "_Layout.cshtml";
}
View1 @Model

.Views_Layout.cshtml

Layout
@RenderBody()

.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AzureFunctionsVersion>v3</AzureFunctionsVersion>
    <_FunctionsSkipCleanOutput>true</_FunctionsSkipCleanOutput>  <-- these 3 properties are important
    <PreserveCompilationReferences>true</PreserveCompilationReferences>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>
  <ItemGroup>
    <None Remove="ViewsView1.cshtml" />
    <None Remove="Views_Layout.cshtml" />
  </ItemGroup>
  <ItemGroup>
    <EmbeddedResource Include="ViewsView1.cshtml">  <-- in my example views are Embedded Resources
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </EmbeddedResource>
    <EmbeddedResource Include="Views_Layout.cshtml">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </EmbeddedResource>
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="3.0.11" />
    <PackageReference Include="RazorLight" Version="2.0.0-rc.3" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Azure功能不提供任何内置或对剃须刀或其他模板引擎的特殊支持,因此您使用的事实不应影响您对使用哪种引擎的决定。

剃须刀和其他模板引擎确实支持编程渲染,因此您应该能够使用该方法使模板在Azure函数中工作。

最新更新