matthijsb
matthijsb

Reputation: 909

Nancy on ASP.NET vNext does not load the Nancy.Viewengines.Razor viewengine

I am trying Nancy with ASP.NET vNext on debian. I've setup a sample project and got it to work with a self hosted Owin application.

Nancy it self seem to run fine but it is looking for the views in the K runtime directory and the Razor viewengine won't load.

K does not show the Nancy.Viewengines.Razor being loaded altho it is in the project.json file

This is the error that I'm getting:

Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'ViewTest' Currently available view engine extensions: sshtml,html,htm Locations inspected: views/Home/ViewTest-nl-NL,views/Home/ViewTest,Home/ViewTest-nl-NL,Home/ViewTest,views/ViewTest-nl-NL,views/ViewTest,ViewTest-nl-NL,ViewTest Root path: /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/ If you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json' at Nancy.ViewEngines.DefaultViewFactory.GetRenderedView (string,object,Nancy.ViewEngines.ViewLocationContext) <0x008a3> .

My project.json:

{ "dependencies": { "Microsoft.Owin.Hosting": "2.1.0-*", "Microsoft.Owin.Hosting": "2.1.0-*", "Microsoft.Owin.Host.HttpListener": "2.1.0-*", "Nancy": "0.23.2-*", "Nancy.Owin": "0.23.2-*", "Nancy.Viewengines.Razor": "0.23.2-*", }, }

Loaded library's:

vnext@vnext:~/test/src$ k run /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/klr.host.dll Information : 0 : [LoaderContainer]: Load name=Microsoft.Owin.Host.HttpListener /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/klr.host.dll Information : 0 : [DefaultLoaderEngine]: LoadFile(/home/vnext/.kpm/packages/Microsoft.Owin.Host.HttpListener/2.1.0/lib/net45/Microsoft.Owin.Host.HttpListener.dll) /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/klr.host.dll Information : 0 : [NuGetAssemblyLoader]: Loaded name=Microsoft.Owin.Host.HttpListener in 1ms /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/klr.host.dll Information : 0 : [LoaderContainer]: Load name=Nancy.Owin /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/klr.host.dll Information : 0 : [DefaultLoaderEngine]: LoadFile(/home/vnext/.kpm/packages/Nancy.Owin/0.23.2/lib/net40/Nancy.Owin.dll) /home/vnext/.kre/packages/KRE-mono45-x86.1.0.0-alpha3/bin/klr.host.dll Information : 0 : [NuGetAssemblyLoader]: Loaded name=Nancy.Owin in 1ms

It will load a normal html view when I place it in the KRE bin directory but not in the project's View folder.

How can I force Nancy to look in the right folder, and how can I load the Razor viewengine?

This is the code that i've used: https://github.com/matthijsbreemans/nancy-owin-vnext

Upvotes: 2

Views: 883

Answers (2)

Luuk
Luuk

Reputation: 1969

Nancy scans the AppDomain.CurrentDomain.BaseDirectory for assemblies by default. In vNext, the assemblies are stored in separate packages, and not in the bin directory, therefore it cannot find the Razor view engine. Until full support is available you can implement your own bootstrapper and override the ViewEngines list:

protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
    base.ConfigureApplicationContainer(container);
    container.Register<IViewEngine, Nancy.ViewEngines.Razor.RazorViewEngine>();
    container.Register<Nancy.ViewEngines.Razor.IRazorConfiguration, Nancy.    ViewEngines.Razor.DefaultRazorConfiguration>();
}

protected override IEnumerable<Type> ViewEngines
{
    get { return new[] { typeof(Nancy.ViewEngines.Razor.RazorViewEngine) }; }
}

The views are also stored in a different directory (especially when published). Create a new class that implements IRootPathProvider, and return the IApplicationEnvironment.ApplicationBasePath. I've done this (for now as a workaround) by storing the IApplicationEnvironment in a static variable (the IoC in Nancy itself can't find an instance when using it in the bootstrapper):

public class Startup
{
    internal static IApplicationEnvironment Environment { get; private set; }

    public Startup(IApplicationEnvironment env)
    {
        Environment = env;
    }

    public void ConfigureServices(IServiceCollection services) { }

    public void Configure(IApplicationBuilder app)
    {
        app.UseOwin(a => a.UseNancy());
    }
}

and implement Nancy's IRootPathProvider:

public class vNextRootPathProvider : IRootPathProvider
{
    private string BasePath = Startup.Environment.ApplicationBasePath;

    public string GetRootPath()
    {
        return BasePath;
    }
}

Upvotes: 3

Maxzhang
Maxzhang

Reputation: 1

KRE can't found the views directory, you can custom the Bootstrapper and override IRootPathProvider , setting the root directory to absolute path. Look at : https://github.com/NancyFx/Nancy/wiki/The-root-path

Upvotes: -1

Related Questions