Search code examples
c#restgrapevine

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


I have setup several routes for a REST api using Grapevine, for a small webserver in a desktop app. The API works fine, and other static files work fine, but I can't get the router to route an empty url: http://:port/ to the root index.html file in the prescribed folder.

Web is a folder in the exe path, containing index.html, and test.html.

I can serve http://xxx:8080/test.html just fine. http://xxx:8080/ gives "Route Not Found For GET /"

Webserver setup:

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

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

Routes:

    [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 needs to serve when the root url is requested.


Solution

  • This works:

    add a route that handles '/' and return the file manually.

    many thanks for your help. I can now use the Nuget package and not worry about a hacked local copy of the lib.

    Fantastic library BTW, I nearly dropped using it because the examples and wiki are so sparse I couldn't figure anything out. Very glad I persevered. Much simpler than everything else I saw (after figuring it out).

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