gpt4 book ai didi

c# - 将 Wyam 嵌入到 asp.net core MVC 解决方案中的正确方法是什么?

转载 作者:太空狗 更新时间:2023-10-29 17:59:52 24 4
gpt4 key购买 nike

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

由于该项目需要高级身份验证,我将其嵌入到 MVC 中。我目前正在将它嵌入一个 MVC Controller ,该 Controller 使用 Controller 读取生成的 html 文件并通过 View 呈现它。

文件以下列方式提供

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

View 只是将 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);

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

最佳答案

根据文档

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

While Wyam is usually executed from the command line application, this is just a thin wrapper around a core library that you can include in your own applications. The core Wyam library is available on NuGet as Wyam.Core. Once you've included it in your application, you will need to create an instance of the Wyam.Core.Execution.Engine class.

To configure the engine, you use the properties of the Engine class.
...
...
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.

理想情况下,由于您要嵌入它,因此您必须手动启动生成过程。

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

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

关于c# - 将 Wyam 嵌入到 asp.net core MVC 解决方案中的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56291170/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com