Nancy: Serving static content (e.g. index.html) from “/”?

不羁岁月 提交于 2019-12-23 06:58:56

问题


I'm trying to make a single page web application using Nancy. Therefore, I want my root URL to serve a plain .html file, without any view logic or whatsoever.

I tried

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

But there's no 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.

What do I do? This shouldn't be this hard, right?

ps. Any way to turn that directory listing off? It feels insecure.


回答1:


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.




回答2:


Add a module that serves index.html by default:

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);
}


来源:https://stackoverflow.com/questions/11817283/nancy-serving-static-content-e-g-index-html-from

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!