自托管Nancy实例返回404错误

时间:2015-10-01 13:26:28

标签: nancy

我正在尝试运行一个自托管的Nancy应用程序,但我无法让它返回有效的响应。我是南希的新人;我希望我的问题很简单。

以下是一些代码:

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]" 
            };
        }
    }
}

我的NancyModule看起来像这样:

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.");
    }
}
南希正在奔跑;我可以在http://127.0.0.1:8880/download/_Nancy/访问诊断程序。看着他们,路线似乎准备好了。 Interactive Diagnostics / GetAllRoutes显示:

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

然而,当我尝试http://localhost:8880/download/时,我正在回复404。

诊断页面上的请求跟踪显示:

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

那么为什么南希没有将这个请求路由到正确的路线?

1 个答案:

答案 0 :(得分:3)

jancy在Nancy JabbR房间向我指出了问题:

URI指定http://localhost:{0}/download/,而模块还指定/download的基本路径,因此目前正在寻找http://localhost:{0}/download/download/

的网址