繁体   English   中英

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

[英]What is the correct way to embed Wyam into an asp.net core MVC solution?

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

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

@Html.Raw(Model.HtmlContent)

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

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

根据文档

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依赖项的干净方法,所以我只是抓住了Wyam.Docs.Samson nuget包的contents文件夹(剃刀文件和其他Web东西),并将其转储到我的Markdown东西旁边

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功能中使用此功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM