簡體   English   中英

找不到GET的路由/-無法提供默認的index.html

[英]Route Not Found For GET / - Can't serve default index.html

我已經為Grapevine為台式機應用程序中的小型Web服務器設置了一些REST API的路由。 該API可以正常工作,其他靜態文件也可以正常工作,但是我無法讓路由器將空網址:http://:port /路由到指定文件夾中的根index.html文件。

Web是exe路徑中的一個文件夾,包含index.html和test.html。

我可以正常投放http:// xxx:8080 / test.html http:// xxx:8080 /給出“找不到GET的路由”

網絡服務器設置:

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

        server = new RestServer(settings);
        server.Start();

路線:

    [RestResource]
    public class WebRequestHandler
    {
        [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/api/v1/live")]
        public IHttpContext Live(IHttpContext context)
        {
            snip
            return context;
        }

        [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/api/v1/cmd1/[id]")]
        public IHttpContext Cmd1(IHttpContext context)
        {
            return context;
        }

        [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/api/v1/cmd2/[id]")]
        public IHttpContext Cmd2(IHttpContext context)
        {
            snip
            return context;
        }

        [RestRoute(HttpMethod = HttpMethod.GET, PathInfo = "/api/v1/cmd3/[id]")]
        public IHttpContext Cmd3(IHttpContext context)
        {
            snip
            return context;
        }


    }

當請求根網址時,index.html需要提供服務。

這有效:

添加一個處理“ /”的路由並手動返回文件。

非常感謝您的幫助。 現在,我可以使用Nuget包,而不必擔心該庫的本地副本被黑。

棒極了的BTW,我差點放棄使用它,因為示例和Wiki如此稀疏,我什么都找不到。 我堅持很高興。 比我所看到的一切都簡單得多(弄清楚之后)。

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

最新的nuget 4.1.1是否存在相同的問題? 和github截至19/5/19

我在這里修改了lib以進行此更改:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM