Search code examples
c#razornancyrazorengine

Using Razor View Engine with Nancy, .cshtml not an available extension


I’m working on a project using Nancy on a Mac, and I’m editing my project using a standard text editor (Atom). I would like to use the Razor View Engine, and I’m trying to figure out how.

I included

  • Nancy.Viewengines.Razor": "1.3.0" in the dependencies in my project.json file

  • using Nancy.ViewEngines.Razor; at the top of my HomeModule.cs file

  • @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<nancytest.Objects.Task> at the top of the View I would like to use Razor in (task_added.cshtml), which has an extension of .cshtml

But when I load up the project, I get the error:

Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'task_added.cshtml' Currently available view engine extensions: sshtml,html,htm```

Is there anything else I should include for it to recognize .cshtml?


Solution

  • Got it to work! Unfortunately, the view engine is built on code that will not run on a Mac. This solution only works on Windows.

    I added the following code to my configuration file (Startup.cs), translated from the Razor Engine Wiki.

    public class RazorConfig : IRazorConfiguration
    {
        public IEnumerable<string> GetAssemblyNames()
        {
            return null;
        }
    
        public IEnumerable<string> GetDefaultNamespaces()
        {
            return null;
        }
    
        public bool AutoIncludeModelNamespace
        {
            get { return false; }
        }
    }
    

    The line @inherits Nancy.ViewEngines.Razor.NancyRazorViewBase<nancytest.Objects.Task> at the top of each View is also unnecessary.