A standalone Nancy instance returning 404 errors

I am trying to run a standalone Nancy application, but I am having problems returning valid answers. I am new to Nancy; I expect my problem to be pretty simple.

Here is the 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 working; I can access the diagnostics in http://127.0.0.1:8880/download/_Nancy/. Looking at them, the routes look 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 get 404s back when I try http://localhost:8880/download/.

The request trace on the diagnostic 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 doesn't Nancy direct this request to the correct route?

+4
source share
1 answer

jchannon :

URI http://localhost:{0}/download/, /download, URL- http://localhost:{0}/download/download/

+3

Source: https://habr.com/ru/post/1609801/


All Articles