Question

I have the following code:

public class Program : NancyModule
{
    static void Main(string[] args)
    {
        using (var host = new NancyHost(new Uri("http://localhost:444"), new CustomConventionsBootstrapper()))
        {
            host.Start();
            Console.ReadLine();
        }
    }

    public Program()
    {
        Get["/"] = parameter =>
        {
            dynamic var = new ExpandoObject();
            var.Test = "Lol";
            return View["RazorView.cshtml", var];
        };
    }
}

public class CustomConventionsBootstrapper : DefaultNancyBootstrapper
{
    protected override void ConfigureApplicationContainer(TinyIoCContainer container)
    {
        base.ConfigureApplicationContainer(container);
        //This should be the assembly your views are embedded in

        var assembly = Assembly.GetEntryAssembly();

        ResourceViewLocationProvider.RootNamespaces.Add(assembly, "NancyTest.Views");


    }

    protected override NancyInternalConfiguration InternalConfiguration
    {
        get
        {
            var res = base.InternalConfiguration;
            res.ViewLocationProvider = typeof(ResourceViewLocationProvider);
            return res;
        }
    }

    void OnConfigurationBuilder(NancyInternalConfiguration x)
    {
        x.ViewLocationProvider = typeof(ResourceViewLocationProvider);
    }

}

And I have RazorView.cshtml in a folder Views in my project set as embedded resource, however each time I open the page it will give me

   Nancy.RequestExecutionException: Oh noes! ---> Nancy.ViewEngines.ViewNotFoundException: Unable to locate view 'RazorView.cshtml'
Currently available view engine extensions: sshtml,html,htm,cshtml,vbhtml
Locations inspected: views/Program/RazorView.cshtml-en-GB,views/Program/RazorView.cshtml,Program/RazorView.cshtml-en-GB,Program/RazorView.cshtml,views/RazorView.cshtml-en-GB,views/RazorView.cshtml,RazorView.cshtml-en-GB,RazorView.cshtml
Root path: C:\Users\Student\documents\visual studio 2013\Projects\NancyTest\NancyTest\bin\Debug
If you were expecting raw data back, make sure you set the 'Accept'-header of the request to correct format, for example 'application/json'
   bij Nancy.ViewEngines.DefaultViewFactory.GetRenderedView(String viewName, Object model, ViewLocationContext viewLocationContext)
   bij Nancy.ViewEngines.DefaultViewFactory.RenderView(String viewName, Object model, ViewLocationContext viewLocationContext)
   bij Nancy.Responses.Negotiation.ViewProcessor.Process(MediaRange requestedMediaRange, Object model, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.NegotiateResponse(IEnumerable`1 compatibleHeaders, Object model, Negotiator negotiator, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.ProcessAsNegotiator(Object routeResult, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.InvokeRouteWithStrategy(Object result, NancyContext context)
   bij Nancy.Routing.DefaultRouteInvoker.<>c__DisplayClass9.b__5(Task`1 completedTask)
   --- Einde van intern uitzonderingsstackpad ---
   bij Nancy.NancyEngine.InvokeOnErrorHook(NancyContext context, ErrorPipeline pipeline, Exception ex)

It is worth noting that this only happens when using Razor views. When I use a simple html file it can find those files fine. I even tried to run old example projects found online on the current (0.22.2) Nancy version, but no luck there either. What's going on?

Thanks

Was it helpful?

Solution

Okay figured this out, but I have to say this is kinda stupid. Checked the source, this is the culprit:

ResourceAssemblyProvider.cs line 31

private static IEnumerable<Assembly> GetFilteredAssemblies()
{
    return AppDomainAssemblyTypeScanner.Assemblies
       .Where(x => !x.IsDynamic)
       .Where(x => !x.GetName().Name.StartsWith("Nancy", StringComparison.OrdinalIgnoreCase));
}

My assembly is called 'NancyTest'. Changed to 'TestNancy', works now. I suggest finding a better way of excluding your own assemblies.

OTHER TIPS

Also, when working with embedded views, one can easily forget to add the view as an embedded resource (a common gotcha that took me a while to get used to):

Right click on view-file -> properties -> Build Action -> select "Embedded Resource"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top