【问题标题】:What is the correct way to embed Wyam into an asp.net core MVC solution?将 Wyam 嵌入 asp.net 核心 MVC 解决方案的正确方法是什么?
【发布时间】:2019-10-10 23:16:15
【问题描述】:

将 Wyam 嵌入到 asp.net 核心 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)

Markdown 生成是通过引擎实例的实例化并将其转换为 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);

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

【问题讨论】:

  • 刚刚在文档wyam.io/docs/usage/embedding 中看到了这一点。 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.

标签: c# asp.net-mvc wyam


【解决方案1】:

根据文档

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);
});

【讨论】:

  • 这个和问题里的代码基本一样。这里的问题是,引擎在添加管道之前执行,它想要加载不可用的 Razor 插件。我的实现的问题是它忽略了配方,会更新问题。
【解决方案2】:

我花了几个小时才得到一些“不错”的东西。我想不出一个干净的方法来连接 Razor 依赖项,所以我只是抓住了 Wyam.Docs.Samson nuget 包(剃须刀文件和其他网络内容)的 contents 文件夹并将其转储到我的降价文件旁边

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:\temp\wyam.test\content"));
            engine.FileSystem.InputPaths.Add(new DirectoryPath(@"C:\temp\wyam.test\input"));
            engine.FileSystem.OutputPath = new DirectoryPath(@"C:\temp\wyam.test\site");
            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 函数中使用它。

【讨论】:

    猜你喜欢
    • 2014-04-08
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多