Nancy Self-Host 404错误

问题描述 投票:1回答:1

我正在尝试构建一个简单的Nancy自托管主机。如果发送正确的路径,则可以正常工作,但是如果在端口号之后发送空路径,则得到的是错误404。如果发送的路径无效,则会出现错误500。我想要的是通用的Get每当发送带有无效路径的请求时都会使用该属性。

这是我的program.cs

using System.Diagnostics;
using Nancy;
using Nancy.Hosting.Self;

namespace NancyDataService
{
    class Program
    {
        static void Main(string[] args)
        {
            var uri = new Uri("http://localhost:8080");
            var hostConfig = new HostConfiguration();
            hostConfig.UrlReservations.CreateAutomatically = true;
            hostConfig.RewriteLocalhost = false;
            using (var nancyHost = new NancyHost(uri, new DefaultNancyBootstrapper(), hostConfig))
            {
                nancyHost.Start();

                Console.WriteLine("Nancy now listening on http://localhost:8080. Press enter to stop");
                try
                {
                    Process.Start("http://localhost:8080");
                }
                catch (Exception)
                {
                }
                Console.ReadKey();
            }

            Console.WriteLine("Stopped. Good bye!");
        }
    }
}

这是我的主要模块:

using Nancy;

namespace NancyDataService
{
    public class MainModule: NancyModule
    {
        public MainModule()
        {
            string json_error = @"{""status"":""fail"",""reason"":""{0}""}";
            string json;

            Get("test", parms =>
            {
                return "test";
            });

            // this is default if desired path not sent
            Get("{parm1}", parms =>
            {
                json = string.Format(json_error, "Invalid method name supplied");
                //return (Response)json;
                return json;
            });
        }
    }
}

我更改了Get语法以匹配Nancy 2.0的方式。我期望上面代码中的最后一个Get会被处理,并给我一个默认的错误消息。如果在浏览器中输入http://localhost:8080/,则会收到错误404响应。如果我输入http://localhost:8080/test,它可以正常工作。如果输入http://localhost:8080/anythingElse,则会出现错误500,内部服务器错误。

我想在端口号之后输入一个“默认”获取部分,以便输入任何意外路径(包括完全不包含路径),它将采用该分支。

BTW,这是针对.Net Core 3.0,Nancy说这可能行不通。我的Nancy.Hosting.Self包中的警告有一个警告,说它是使用.Net Framework(4.6.1-4.8)还原的。这可能是问题吗?

任何想法如何使它起作用?谢谢...

nancy self-hosting
1个回答
0
投票
已解决大多数问题。空路径需要Get("/", ...,不良路径需要Get("/{parm}",...。似乎string.Format导致错误500,但json_error.Replace("{0}", "new text")正常运行。这是怎么回事?
© www.soinside.com 2019 - 2024. All rights reserved.