Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 933
antpaw Avatar asked Jun 21 '15 14:06

antpaw


2 Answers

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.

like image 171
Jagadeesh Govindaraj Avatar answered Nov 13 '22 06:11

Jagadeesh Govindaraj


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? enter image description here

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?

like image 35
Magnus Karlsson Avatar answered Nov 13 '22 06:11

Magnus Karlsson