Nancy icon indicating copy to clipboard operation
Nancy copied to clipboard

2.0-barney Inconsistent mapping of views in ResourceViewLocationProvider

Open dk-ok opened this issue 8 years ago • 1 comments

Prerequisites

  • [x] I have written a descriptive issue title
  • [x] I have verified that I am running the latest version of Nancy
  • [x] I have verified if the problem exist in both DEBUG and RELEASE mode
  • [x] I have searched open and closed issues to ensure it has not already been reported

Description

This code Nancy.ViewEngines.ResourceViewLocationProvider.RootNamespaces.Add(a, name); maps view paths differently depending on when it is called. If called before WebApp.Start(), it behaves exactly as described, and allows nice shortcut references to embedded views. But if called after (i.e. app.UseNancy() has been called), the mapped paths are much longer, almost the full path (except what might be common with other library names).

I prefer the second method so I don't have to hard-wire references to secondary libs containing views. I want them registered dynamically without foreknowledge.

I've been able to mimic the second by Assembly.ReflectionOnlyLoad and looking for Modules or Views, and then adding to RootNamespaces, all before WebApp.Start(), but it seems like the mapping should be consistent either way.

On a secondary note, ResourceViewLocationProvider requires a registered assembly to have at least two views, otherwise an InvalidOperationException occurs. I have to add a dummy view to make it happy. Am I supposed to assign a "rootnamespace"?

Steps to Reproduce

Call Nancy.ViewEngines.ResourceViewLocationProvider.RootNamespaces.Add(a, name) on a number of assemblies both before and after Nancy has started. Note that paths to views are different depending on where the method is called.

System Configuration

  • Nancy version: 2.0
  • Nancy host
    • [ ] ASP.NET
    • [x] OWIN
    • [ ] Self-Hosted
    • [ ] Other:
  • Other Nancy packages and versions:
  • Environment (Operating system, version and so on): VS2015, Win 7
  • .NET Framework version: 4.5.2
  • Additional information:

dk-ok avatar Sep 14 '16 20:09 dk-ok

Regarding your second point, I can confirm that having only one view in an assembly causes a System.InvalidOperationException with the text: Only one view was found in assembly ..., but no rootnamespace had been registered.

I've gotten around it by copying ResourceViewLocationProvider locally and commenting out:

if (resourceStreams.Count() == 1 && !RootNamespaces.ContainsKey(assembly))
{
    var errorMessage = string.Format("Only one view was found in assembly {0}, but no rootnamespace had been registered.", assembly.FullName);
     
    throw new InvalidOperationException(errorMessage); 
}

I tried assigning a root namespace to the assembly but it didn't make a difference.

dineshpannu avatar Apr 13 '18 05:04 dineshpannu