南希:从“/”提供静态内容(例如index.html)?

时间:2012-08-05 14:38:38

标签: c# nancy

我正在尝试使用Nancy创建单页Web应用程序。因此,我希望我的根URL能够提供一个普通的.html文件,没有任何视图逻辑或者无论如何。

我试过

Get["/"] = parameters => Response.AsHtml("content/index.html")

但是没有AsHtml

我尝试了一个带

的自定义引导程序
conventions.StaticContentsConventions.Add(
    StaticContentConventionBuilder.AddFile("/", @"content/index.html")
);

但显然它认为&#34; /&#34;不是文件 - 南希给了我http://localhost:<port>/上的目录列表。

我该怎么办?这不应该这么难,对吧?

PS。任何方式关闭该目录列表?感觉不安全。

2 个答案:

答案 0 :(得分:20)

默认情况下添加一个提供index.html的模块:

public IndexModule() : base("")
{
    Get[@"/"] = parameters =>
    {
        return Response.AsFile("Content/index.html", "text/html");
    };
}

然后使用引导程序中的约定来提供其余文件:

protected override void ConfigureConventions(NancyConventions nancyConventions)
{
    nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("/", "Content"));
    base.ConfigureConventions(nancyConventions);
}

答案 1 :(得分:4)

只需将其放入您的观看文件夹即可:

Get["/"] = _ => View["index"];

目录列表与Nancy无关,无论你正在使用什么托管都显示它。

相关问题