如何在 Asp.net MVC 6 中使用 NHaml(找不到模板错误)

How to use NHaml in Asp.net MVC 6 (template not found error)

首先我要说的是,我对Asp.net的经验非常少,这就是为什么这个问题在某些地方可能会令人困惑。 我首先通过 yo aspnet 创建了 https://code.visualstudio.com/Docs/ASPnet5 一个 MVC6 项目。然后我将这一行添加到 project.json

中的“依赖项”
"Microsoft.Framework.ConfigurationModel.Xml": "1.0.0-beta4",
"System.Web.NHaml.Mvc3": "4.0.8",

然后我用 this Google code content 创建了 haml.xml 并在 Startup.cs 文件中扩展了以下行:

var configuration = new Configuration()
            .AddJsonFile("config.json")
            .AddXmlFile("haml.xml")
            .AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);

这是我的控制器的样子:

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View(@"~/Views/Shared/Test.haml");
    }
}

应用程序启动,但我无法呈现操作,这是浏览器中显示的错误

An unhandled exception occurred while processing the request.

InvalidOperationException: The view '~/Views/Shared/Test.haml' was not found. The following locations were searched: ~/Views/Shared/Test.haml. Microsoft.AspNet.Mvc.Rendering.ViewEngineResult.EnsureSuccessful () [0x00000] in , line 0

但是这个文件确实存在于这个目录中。感谢任何提示或帮助。


赏金编辑:

基本上我想要的是一个非常非常简单的 MVC 5 或 6 项目的详细解释或工作示例,该项目使用 NHaml 或任何其他提供 Haml 语法的库(如果它在 osx 上与 Mono 一起工作,这是一个奖励)。坦克很多!

我认为是 Razor 视图引擎引发了该错误。

在使用 asp.net 5 MVC6 获得 NHaml 视图引擎时,我无法让它工作,因为它需要我重写它以从 IViewEngine 继承。如果您尝试使用启用了调试器的 Visual Studio 代码编辑器,我认为这是您应该收到的错误。可以试试Visual Studio 2013 Community or VS 2015 Trial version看看会不会出现下面的错误?

这是您当前的 ViewEngine 现在出现故障的地方,给人一种丢失文件的印象。 Source code for OP provided error message 如果将 haml 文件更改为 cshtml 文件是否有效?

我想说的是 Haml 不支持 MVC5 和 MVC 6

因为 Haml 视图引擎项目可能 discontinued. the last commit appeared in Feb 25, 2013.. Also HAML view engine issues are not resolved today itself.The last realease of Haml View engine in nuget 在 2013 Feb.So 这没有意义。

所以你改用 Spark View engine 它也支持 Mono..

Also spark have support "left-offset" syntax inspired by frameworks like Jade and Haml

项目文档是here

您还安装了来自 Nuget

的二进制文件

Spark 还提供配置视图引擎的示例。

祝你好运...

编码愉快。

还有另一种选择,但适用于 ASP.NET Core。从 github 网站 https://github.com/AspNetMonsters/pugzor

见下文

Pugor 是 ASP.NET Core 的 Pug 视图引擎。您可能知道哈巴狗以前的名字 Jade。您可以在他们的网站上阅读更多关于 Pug 的信息:https://pugjs.org/api/getting-started.html

Pugzor 的工作原理是通过 Steve Sanderson 出色的 JavaScript 服务将您的模型直接传递给 pug 的节点版本。它的性能实际上非常好,还有许多优化工作要做。