简体   繁体   English

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

[英]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.

相关问题 Nancy响应未返回index.html的正确MIME类型,包括 - Nancy response not returning proper MIME Type for index.html including 将html解码(例如&gt;和&lt;)转换为普通可读文本(例如&gt;和&lt;) - Convert html decode (e.g. &gt; and &lt;) to a normal readable text( e.g. > and <) 使用Nancy 2.0(clinteastwood)提供嵌入的静态文件 - Serving embeded static files with Nancy 2.0 (clinteastwood) .NET Core Nancy应用程序提供静态文件 - .NET Core Nancy application serving static files ASP.NET 5项目不提供index.html - ASP.NET 5 Project not serving index.html XDocument将不会解析html实体(例如),但XmlDocument将 - XDocument will not parse html entities (e.g. &#xC;) but XmlDocument will Nancy在单独的库中提供模块的视图 - Nancy serving views from module in separate library 从 ASP.NET CORE Web Api 获取 React 中的特定响应标头(例如,Content-Disposition) - Get a specific response header (e.g., Content-Disposition) in React from an ASP.NET CORE Web Api 如何从文件中删除单个属性(例如只读)? - How to remove a single Attribute (e.g. ReadOnly) from a File? 如何从列表框中获取项目容器(例如stackpanel) - How to get item container (e.g. stackpanel) from listbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM