簡體   English   中英

Nancy在單獨的庫中提供模塊的視圖

[英]Nancy serving views from module in separate library

我是Nancy的新手,我正在嘗試在單獨的項目中為每個模塊/控制器設置一個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添加前綴。

您需要在引導程序中配置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/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM