C# 在单独的库中提供来自模块的视图
我是Nancy的新手,我正在尝试在单独的项目中为每个模块/控制器设置一个webapp。主项目是空的ASP.NET项目,并使用C# 在单独的库中提供来自模块的视图,c#,asp.net,razor,nancy,C#,Asp.net,Razor,Nancy,我是Nancy的新手,我正在尝试在单独的项目中为每个模块/控制器设置一个webapp。主项目是空的ASP.NET项目,并使用Nancy.Hosting.Aspnetnuget包 有这种设置的优雅方式是什么 假设我有以下解决方案结构: /ModuleA - ModuleA.csproj - IndexA.cshtml (Copy to Output Directory = Copy Always) /MainModule (references ModuleA) - MainModule.csp
Nancy.Hosting.Aspnet
nuget包
有这种设置的优雅方式是什么
假设我有以下解决方案结构:
/ModuleA
- ModuleA.csproj
- IndexA.cshtml (Copy to Output Directory = Copy Always)
/MainModule (references ModuleA)
- MainModule.csproj
- Index.cshtml
/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
目前,要从
ModuleA
提供IndexA
视图,我必须编写view[“bin/IndexA”]
,这看起来很难看,因为它还需要以同样的方式为javascript/css添加前缀。您需要在引导程序中配置nancy约定。这是南希医生:
鉴于此解决方案结构:
/ModuleA
- ModuleA.csproj
- IndexA.cshtml (Copy to Output Directory = Copy Always)
/MainModule (references ModuleA)
- MainModule.csproj
- Index.cshtml
/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
在main模块
引导程序中:
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在评论中提到的,您也可以使用视图另存为资源,但这超出了我的回答范围。这是一篇关于这个主题的好文章:我以前没有这样做过,但请尝试在引导程序中添加到
ResourceViewLocationProvider.RootNamespaces
。类似于ResourceViewLocationProvider.RootNamespaces.Add(typeof(ModuleA.Assembly,“ModuleA”)代码>。您可以轻松地循环所有引用的模块,并在运行时添加它们。别忘了注册提供商(NancyInternalConfiguration.ViewLocationProvider
)谢谢,听起来很有希望!有机会的时候我会试试的。