2014-07-05 29 views
3

我是南希的新手,我试图在单独的项目中为每个模块/控制器设置一个webapp。主项目是空的ASP.NET项目并使用Nancy.Hosting.Aspnet nuget包。南希从单独的库中的模块中提供视图

有这种设置的优雅方式是什么?

说我有以下解决方案结构:

/ModuleA 
- ModuleA.csproj 
- IndexA.cshtml (Copy to Output Directory = Copy Always) 

/MainModule (references ModuleA) 
- MainModule.csproj 
- Index.cshtml 

目前从ModuleA我发球IndexA观点写View["bin/IndexA"],这似乎很丑陋,因为它也需要以同样的方式前缀的JavaScript/CSS。

+0

我以前没有这样做过,但是尝试在你的引导程序中添加'ResourceViewLocationProvider.RootNamespaces'。类似于'ResourceViewLocationProvider.RootNamespaces.Add(typeof(ModuleA).Assembly,“ModuleA”);'。您可以轻松地遍历所有引用的模块并在运行时添加它们。不要忘记注册提供程序('NancyInternalConfiguration.ViewLocationProvider') – eth0

+0

谢谢,这听起来很有希望!当我有机会时,我会尝试一下。 – Grozz

回答

0

您需要在引导程序中配置nancy约定。这里是南希的文档:https://github.com/NancyFx/Nancy/wiki/View-location-conventions

鉴于这种解决方案结构:

/ModuleA 
- ModuleA.csproj 
- views/IndexA.cshtml (Copy to Output Directory = Copy Always) 
- assets/foo.js (Copy to Output Directory = Copy Always) 

/MainModule (references ModuleA) 
- MainModule.csproj 
- Index.cshtml 

MainModule引导程序:

public class Bootstrapper : DefaultNancyBootstrapper 
{ 
    protected override void ConfigureConventions(Nancy.Conventions.NancyConventions nancyConventions) 
    { 
     base.ConfigureConventions(nancyConventions); 

     // for views in referenced projects 
     nancyConventions.ViewLocationConventions.Add(
      (viewName, model, context) => string.Concat("bin/views/", viewName)); 

     // for assets in referenced projects   
     nancyConventions.StaticContentsConventions.Add(
      Nancy.Conventions.StaticContentConventionBuilder.AddDirectory("assets", "bin/assets")); 
    } 
} 

IndexA.cshtml

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <script src="/assets/foo.js"></script> 
    </head> 
    <body></body> 
</html> 

正如在评论中提到通过@ eth0的,你也可以使用视图保存为资源,但这超出了我的回答范围河这里是关于这个主题的好文章:http://colinmackay.scot/2013/05/02/configuring-the-nancy-to-use-views-in-a-separate-assembly/