C# 找不到GET/-Can';t serve default index.html

C# 找不到GET/-Can';t serve default index.html,c#,rest,grapevine,C#,Rest,Grapevine,我已经使用Grapevine为RESTAPI设置了几个路由,用于桌面应用程序中的小型Web服务器。API工作正常,其他静态文件工作正常,但我无法让路由器将空url:http://:port/路由到指定文件夹中的root index.html文件 Web是exe路径中的文件夹,包含index.html和test.html 我的发球很好。 给出“未找到GET/的路由” Web服务器设置: ServerSettings settings = new ServerSettings()

我已经使用Grapevine为RESTAPI设置了几个路由,用于桌面应用程序中的小型Web服务器。API工作正常,其他静态文件工作正常,但我无法让路由器将空url:http://:port/路由到指定文件夹中的root index.html文件

Web是exe路径中的文件夹,包含index.html和test.html

我的发球很好。 给出“未找到GET/的路由”

Web服务器设置:

        ServerSettings settings = new ServerSettings()
        {
            Host = "*",
            Port = "8080",
            PublicFolder = new PublicFolder("Web")                
        };

        server = new RestServer(settings);
        server.Start();
路线:

[RestResource]
公共类WebRequestHandler
{
[重新路由(HttpMethod=HttpMethod.GET,PathInfo=“/api/v1/live”)]
公共IHttpContext Live(IHttpContext上下文)
{
剪
返回上下文;
}
[重新路由(HttpMethod=HttpMethod.GET,PathInfo=“/api/v1/cmd1/[id]”)
公共IHttpContext Cmd1(IHttpContext上下文)
{
返回上下文;
}
[重新路由(HttpMethod=HttpMethod.GET,PathInfo=“/api/v1/cmd2/[id]”)
公共IHttpContext Cmd2(IHttpContext上下文)
{
剪
返回上下文;
}
[重新路由(HttpMethod=HttpMethod.GET,PathInfo=“/api/v1/cmd3/[id]”)
公共IHttpContext Cmd3(IHttpContext上下文)
{
剪
返回上下文;
}
}

当请求根url时,index.html需要提供服务。

最新的nuget 4.1.1有相同的问题吗?截至19年5月31日和github

我在此处修改了此更改的库:

protected void AddToDirectoryList(string fullPath)
        {
            DirectoryList[CreateDirectoryListKey(fullPath)] = fullPath;
            if (fullPath.EndsWith($"\\{_indexFileName}"))
            {
                DirectoryList[CreateDirectoryListKey(fullPath.Replace($"\\{_indexFileName}", ""))] = fullPath;
+++             DirectoryList["/"] = fullPath;
            }
        }
这项工作:

添加处理“/”并手动返回文件的路由

非常感谢你的帮助。我现在可以使用Nuget包,而不用担心库的本地副本被黑客攻击

顺便说一句,我差点放弃使用它,因为示例和wiki太少了,我什么都想不出来。很高兴我坚持了下来。比我所看到的一切都要简单得多


显示rest API的路由定义添加了路由定义您使用的是哪种版本的葡萄藤?@ScottOffen 4.1.1 nuget和githib 31/5/19我发布了一个答案,其中我破解了一个解决方案。这意味着chrome将以“/”的形式发送url,而直接列表的根文件夹键为空。所以它与index.htmlI的“/”不匹配。我删除了我的答案,因为它完全不准确(正如您所指出的)。如果你想提交一个问题,我可以添加一个功能来处理这个问题。谢谢,我会这样做的,它可以被跟踪。谢谢你的帮助。是的,这种方式对我来说很笨拙,所以我把它从我(现在删除的)答案中删掉了。我确实需要完成文档。
        [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/")]
        public IHttpContext Root(IHttpContext context)
        {
            var filepath = Path.Combine(context.Server.PublicFolder.FolderPath, 
                                        context.Server.PublicFolder.IndexFileName);

            var lastModified = File.GetLastWriteTimeUtc(filepath).ToString("R");
            context.Response.AddHeader("Last-Modified", lastModified);

            if (context.Request.Headers.AllKeys.Contains("If-Modified-Since"))
            {
                if (context.Request.Headers["If-Modified-Since"].Equals(lastModified))
                {
                    context.Response.SendResponse(HttpStatusCode.NotModified);
                    return context;
                }
            }

            context.Response.ContentType = ContentType.DEFAULT.FromExtension(filepath);
            context.Response.SendResponse(new FileStream(filepath, FileMode.Open));

            return context;
        }