简体   繁体   中英

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

First I need to say that my experience with Asp.net is very small, that's why this question could come of confusing in some parts. I've first created https://code.visualstudio.com/Docs/ASPnet5 a MVC6 project via yo aspnet . Then I've added this line to "dependencies" in project.json

"Microsoft.Framework.ConfigurationModel.Xml": "1.0.0-beta4",
"System.Web.NHaml.Mvc3": "4.0.8",

Then I've created haml.xml with this Google code content and extended the following line in the Startup.cs file:

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

this is how my controller looks like:

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

the apps starts up, but I can't render the action, this is the error that shows up in the browser

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

But this file does exist in this directory. Any hints or help is appreciated.


BOUNTY edit:

Basicly what I want is a detailed explanation or a working example of a very very simple MVC 5 or 6 Project that uses NHaml or any other lib that Provides Haml syntax (If it works with Mono on osx, that's a bonus). Tanks a lot!

I was wondering to say this there is no support from Haml for MVC5 AND MVC 6

beacause The Haml View Engine Project maybe 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 at 2013 Feb.So this not make sense.

So instead you Use Spark View engine its support Mono also..

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

The project documentation is here

you also install the binaries from Nuget

Also spark provide samples for configure view engines.

Good Luck...

Happy coding.

I think it is the Razor view engine throwing that error.

As of getting an NHaml view engine with asp.net 5 MVC6 I could not manage to get that working since it would require me to rewrite it to inherit from IViewEngine. This is the error I think you should have received if you try with a debugger enabled Visual Studio code editor. Could you try Visual Studio 2013 Community or VS 2015 Trial version to see if you get the following error? 在此输入图像描述

This is where your current ViewEngine fails right now giving the impression of a missing file. Source code for OP provided error message Does it work if you change your haml file to a cshtml file?

There is another alternative, but for ASP.NET Core. See below from the github site https://github.com/AspNetMonsters/pugzor

Pugor is the Pug view engine for ASP.NET Core. You might know pug by its previous name, Jade. You can read more about Pug over at their website: https://pugjs.org/api/getting-started.html

Pugzor works by passing your models directly to the node version of pug via Steve Sanderson's glorious JavaScript services. It is actually pretty performant and there are many optimizations still to be made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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