Nancy uses the Razor view engine: built-in views do not work

advertisements

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


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.