Bor*_*ash 5 c# azure asp.net-core azure-functions
我正在创建一个应用程序,它是 azure 函数项目,我想在该项目中使用 Razor 视图,是否有我应该在 azure 函数中使用的模板引擎?
由于RazorLight 项目和 Azure Functions 的一些进步,现在可以选择渲染 Razor。
警告在此示例中,视图是在运行时编译的。如果您的 Azure Functions 经常停止和启动,请考虑这一点。
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
.\Views\View1.cshtml
@model String;
@{
Layout = "_Layout.cshtml";
}
View1 @Model
Run Code Online (Sandbox Code Playgroud)
.\Views\_Layout.cshtml
Layout
@RenderBody()
Run Code Online (Sandbox Code Playgroud)
.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="Views\View1.cshtml" />
<None Remove="Views\_Layout.cshtml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\View1.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>
Run Code Online (Sandbox Code Playgroud)
Azure Functions 不为 Razor 或其他模板引擎提供任何内置或特殊支持,因此您使用它的事实不应影响您对使用哪个引擎的决定。
Razor 和其他模板引擎确实支持编程渲染,因此你应该能够使用该方法让模板在 Azure Functions 中工作。
| 归档时间: |
|
| 查看次数: |
3907 次 |
| 最近记录: |