将WYAM嵌入ASP.NET Core MVC解决方案中的正确方法是什么?



将WYAM嵌入和ASP.NET Core MVC解决方案的正确方法是什么?

由于项目需要高级身份验证,因此我将其嵌入了MVC中。我目前正在使用MVC控制器读取生成的HTML文件,并通过视图将其渲染。

文件以以下方式提供

public IActionResult Index()
{
    return ServeMarkdownPage("index");
}
[Route("{pageName}")]
public IActionResult ServeMarkdownPage([FromRoute]string pageName)
{
     if (!System.IO.File.Exists($"HtmlOutput//{pageName}.html"))
     {
         return View("Error");
     }
     var content = System.IO.File.ReadAllText($"HtmlOutput//{pageName}.html");
     return View("MarkdownPage", new MarkdownPageViewModel { HtmlContent = content });
}

视图仅将HTML内容输出到页面。

@Html.Raw(Model.HtmlContent)

降压生成是通过实例实例化并将其转换为HTML完成的。在这种情况下,Waym食谱似乎被忽略了。

var engine = new Wyam.Core.Execution.Engine();
engine.FileSystem.InputPaths.Add(new DirectoryPath("Markdown"));
engine.FileSystem.OutputPath = new DirectoryPath("HtmlOutput");
engine.Pipelines.Add(new Pipeline(
    "DocumentationPages",
    new ReadFiles("**/*.md"),
    new FrontMatter(new Yaml()),
    new Markdown(),
    new WriteFiles(".html")));
var docsRecipe = new Docs();
docsRecipe.Apply(engine);

这可以更好地完成吗?配方是否正确调用?

根据文档

https://wyam.io/docs/usage/embedding

通常从命令行应用程序执行了WYAM,但这只是一个核心库周围的薄包装器,您可以将其包含在自己的应用程序中。WYAM核心库可在Nuget上作为Wyam.Core提供。将其包含在应用程序中后,您将需要创建Wyam.Core.Execution.Engine类的实例。

要配置引擎,您可以使用Engine类的属性。
...
...
配置发动机后,请通过调用Engine.Execute()执行。这将开始评估管道,任何输出消息都将发送到配置的跟踪端点。

非常理想地,由于您嵌入了它,因此您必须手动开始生成过程。

您可以创建一个简单的助手来为您管理。

public static class WyamConfiguration {
    public static void Embed(Action<Wyam.Core.Execution.Engine> configure) {
        // you will need to create an instance of the Wyam.Core.Execution.Engine class
        var engine = new Wyam.Core.Execution.Engine();
        // configure the engine
        configure(engine);
        // Once the engine is configured, execute it with a call to Engine.Execute()
        // This will start evaluation of the pipelines and any output messages will 
        // be sent to the configured trace endpoints.
        engine.Execute();
    }
}

并从您的 startup.cs

中调用它

例如

WyamConfiguration.Embed(engine => {
    engine.FileSystem.InputPaths.Add(new DirectoryPath("Markdown"));
    engine.FileSystem.OutputPath = new DirectoryPath("HtmlOutput");
    engine.Pipelines.Add(new Pipeline(
        "DocumentationPages",
        new ReadFiles("**/*.md"),
        new FrontMatter(new Yaml()),
        new Markdown(),
        new WriteFiles(".html")));
    var docsRecipe = new Docs();
    docsRecipe.Apply(engine);
});

花了我几个小时才能获得"好的"。我不知道一种干净的方法来连接剃须刀依赖关系,所以我刚刚抓住了wyam.docs.samson nuget软件包(剃须刀文件和其他网络内容(的contents文件夹,然后将其倒在我的Markdown fumps

using System;
using Wyam.Common.IO;
using Wyam.Core.Execution;
using Wyam.Docs;
namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Wyam.Common.Tracing.Trace.AddListener(new System.Diagnostics.TextWriterTraceListener(Console.Out));
            var engine = new Engine();
            engine.Namespaces.Add("Wyam.Docs"); // or razor will complain
            engine.FileSystem.InputPaths.Add(new DirectoryPath(@"C:tempwyam.testcontent"));
            engine.FileSystem.InputPaths.Add(new DirectoryPath(@"C:tempwyam.testinput"));
            engine.FileSystem.OutputPath = new DirectoryPath(@"C:tempwyam.testsite");
            var dr = new Docs();
            dr.Apply(engine);
            engine.Execute();
        }
    }
}

我的项目文件是以下内容,我相信我必须编辑Wyam.common包,因为它没有正确注册。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Wyam.Common" Version="2.2.5" />
    <PackageReference Include="Wyam.Core" Version="2.2.5" />
    <PackageReference Include="Wyam.Docs" Version="2.2.5" />
  </ItemGroup>
</Project>

使用此设置(VS2019(生成一个站点的外观与使用#recipe Docs或其他任何东西运行Wyam CLI时相同。

编辑:我想这并没有真正回答这个问题,但是希望这有助于将解决方案放在一起。我计划在控制台示例足够起点的Azure函数中使用它。

相关内容

最新更新