[英]Nancy: Serving static content (e.g. index.html) from “/”?
I'm trying to make a single page web application using Nancy. 我正在尝试使用Nancy制作单页Web应用程序。 Therefore, I want my root URL to serve a plain .html file, without any view logic or whatsoever. 因此,我希望我的根URL提供一个普通的.html文件,没有任何视图逻辑或任何东西。
I tried 我试过了
Get["/"] = parameters => Response.AsHtml("content/index.html")
But there's no AsHtml
. 但是没有AsHtml
。
I tried a custom bootstrapper with 我试过一个自定义引导程序
conventions.StaticContentsConventions.Add(
StaticContentConventionBuilder.AddFile("/", @"content/index.html")
);
But apparently it thinks that "/" is not a file - Nancy gives me a directory listing on http://localhost:<port>/
instead. 但显然它认为“/”不是文件 - 南希给我一个目录列表在http://localhost:<port>/
而不是。
What do I do? 我该怎么办? This shouldn't be this hard, right? 这应该不是这么难,对吧?
ps. PS。 Any way to turn that directory listing off? 任何方式关闭该目录列表? It feels insecure. 感觉不安全。
Add a module that serves index.html
by default: 添加默认情况下为index.html
提供服务的模块:
public IndexModule() : base("")
{
Get[@"/"] = parameters =>
{
return Response.AsFile("Content/index.html", "text/html");
};
}
Then serve the rest of your files using a convention in your bootstrapper: 然后使用引导程序中的约定为其余文件提供服务:
protected override void ConfigureConventions(NancyConventions nancyConventions)
{
nancyConventions.StaticContentsConventions.Add(StaticContentConventionBuilder.AddDirectory("/", "Content"));
base.ConfigureConventions(nancyConventions);
}
Just put it your views folder and do: 只需将它放在您的视图文件夹中即可:
Get["/"] = _ => View["index"];
The directory listing is nothing to do with Nancy, whatever hosting you're using is displaying that. 目录列表与Nancy无关,无论您正在使用的托管是什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.