将Wyam嵌入asp.net核心MVC解决方案的正确方法是什么?

Ale*_*dar 9 c# asp.net-mvc wyam

将Wyam嵌入和asp.net核心MVC解决方案的正确方法是什么?

由于该项目需要高级身份验证,因此我将其嵌入到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 });
}
Run Code Online (Sandbox Code Playgroud)

该视图仅将html内容输出到页面中。

@Html.Raw(Model.HtmlContent)
Run Code Online (Sandbox Code Playgroud)

Markdown生成是通过实例化引擎实例并将其转换为html来完成的。在这种情况下,似乎忽略了配方。

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);
Run Code Online (Sandbox Code Playgroud)

能以更好的方式做到这一点吗?配方是否正确使用?

Nko*_*osi 0

根据文档

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并从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);
});
Run Code Online (Sandbox Code Playgroud)