2015-10-01 70 views
4

我試圖讓自己託管的南希應用程序運行,但我無法讓它返回有效的響應。我是南希的新人;我希望我的問題很簡單。自我託管的南希實例返回404錯誤

下面是一些代碼:

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/的診斷信息。看着它們,路線顯得已準備就緒。交互式診斷/ GetAllRoutes顯示:

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

然而,我得到404回來時,我嘗試http://localhost:8880/download/

上診斷頁顯示請求跟蹤:

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 

那麼,爲什麼不南希路由這個請求到正確的路線?

回答

3

問題在南希JabbR室向我指出的jchannon:

的URI指定http://localhost:{0}/download/,同時該模塊還指定/download一個基本路徑,所以目前其正在尋找的http://localhost:{0}/download/download/

的URL