Remi Despres-Smyth
Remi Despres-Smyth

Reputation: 4253

Self-hosted Nancy instance returning 404 errors

I'm trying to get a self-hosted Nancy app running, but I'm having trouble getting it to return valid responses. I'm new at Nancy; I expect my problem is something fairly simple.

Here's some code:

class Program
{
    static void Main(string[] args)
    {
        const String PORT_SETTING = "webServicePortNumber";
        const String URI = "http://localhost:{0}/download/";

        var portNum = ConfigurationManager.AppSettings[PORT_SETTING];
        var uri = new Uri(String.Format(URI, portNum));

        var config = new HostConfiguration { 
            UrlReservations = new UrlReservations { CreateAutomatically = true }
        };

        using (var nancyHost = new NancyHost(new Bootstrapper(), config, uri)) {
            nancyHost.Start();

            Console.WriteLine(String.Format("Listening on {0}. Press any key to stop.", uri.AbsoluteUri));
            Console.ReadKey();
        }

        Console.WriteLine("Stopped. Press any key to exit.");
        Console.ReadKey();
    }
}

internal class Bootstrapper : DefaultNancyBootstrapper
{
    protected override Nancy.Diagnostics.DiagnosticsConfiguration DiagnosticsConfiguration
    {
        get {
            return new DiagnosticsConfiguration { 
                Password = @"[password]" 
            };
        }
    }
}

My NancyModule looks like this:

public class DownloadsModule : NancyModule
{

    public DownloadsModule() : base("/download")
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        Put["/"] = parms => InitiateDownload(parms);
        Get["/"] = parms => Summary(parms);
        Get["/{id}"] = parms => GetStatus(parms.requestId);
    }

    private Response GetStatus(Guid requestId)
    {
        return Response.AsText("TEST: GetStatus requestId " + requestId);
    }

    private Response Summary(dynamic parms)
    {
        return Response.AsText("Summary: You loved me before, do you love me now?");
    }

    private Response InitiateDownload(dynamic parms)
    {
        return Response.AsText("InitiateDownload.");
    }
}

Nancy is running; I can access the diagnostics at http://127.0.0.1:8880/download/_Nancy/. Looking at them, the routes appear ready. Interactive Diagnostics/GetAllRoutes shows:

P U T
    name: [nothing]    path: /download
G E T
    name: [nothing]    path: /download
    name: [nothing]    path: /download/{id}

And yet, I'm getting 404s back when I try http://localhost:8880/download/.

The request trace on the diagnostics page shows:

Method: GET
Request Url:
Scheme: http
Host Name: localhost
Port: 8880
Base Path: /download
Path: /
Query:
Site Base: http://localhost:8880
Is Secure: false
Request Content Type:
Response Content Type: text/html
Request Headers:
    <snip>
    Accept: text/html;q=1
            application/xhtml+xml;q=1
            image/webp;q=1
            application/xml;q=0.9
            */*;q=0.8
    <snip>
Response Headers:
Status Code: 404
Log:    New Request Started
        [DefaultResponseNegotiator] Processing as real response

So why isn't Nancy routing this request to the proper route?

Upvotes: 4

Views: 2048

Answers (1)

Remi Despres-Smyth
Remi Despres-Smyth

Reputation: 4253

Problem pointed out to me by jchannon in the Nancy JabbR room:

The URI specifies http://localhost:{0}/download/, while the module also specifies a base path of /download, so currently its looking for an URL of http://localhost:{0}/download/download/

Upvotes: 3

Related Questions