【问题标题】:Nancy self host returns 404Nancy 自己的主机返回 404
【发布时间】:2017-11-19 18:18:54
【问题描述】:

我有一个控制台应用程序,我想使用 Nancy 来托管 Web 服务。 这是我的 Program.cs

namespace NancyConsole
{
    using Nancy.Hosting.Self;
    using System;
    internal class Program
    {
        private static void Main(string[] args)
        {
            string url = "http://localhost:1234";
            var host = new NancyHost(new Uri(url));
            host.Start();
            Console.WriteLine("Server started, now listening on " + url);
            Console.WriteLine("Press any key to stop the server...");
            Console.ReadKey();
            host.Stop();
        }
    }
}

这是我的 MainModule.cs 文件

namespace NancyConsole
{
    using Nancy;
    internal class MainModule : NancyModule
    {
        public MainModule()
        {
            Get["/"] = x => View["Views/index.html"];
        }
    }
}

这是我的项目结构

当我运行项目时,这是我在浏览器上遇到的错误

我不知道我哪里出错了?非常感谢您的帮助!

【问题讨论】:

  • 您是否尝试以管理员身份运行。也许Vs没有保留端口。是否预留端口:netsh http add urlacl url = http://*:1234/ user=[USER]
  • 我没有在VS上运行,我编译了解决方案然后我去Debug文件夹并以管理员身份运行exe
  • @MRsa 也可以将带有选项 RewriteLocalhost = false 的 HostConfiguration 传递给 NancyHost 构造函数,这样您就不需要设置 url acls。当然,这只会绑定本地主机,其他节点无法访问。

标签: c# nancy self-hosting


【解决方案1】:

公开模块。默认加载策略使用反射来查找公共模块类。

public class MainModule : NancyModule

来自documentation

将模块声明为公开很重要,否则 NancyFx 无法发现您的模块。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2020-04-09
    • 2021-02-10
    • 2015-08-14
    • 2023-04-11
    相关资源
    最近更新 更多