简体   繁体   中英

Hello world app for Nancy Framework does not compile

I'm trying to get a hello world web application running with Nancy. It does not compile on and highlights the Get["/"] = parameters => "Hello World"; and does not recognize this symbol.

I must be missing something simple but can't find anything on the web on this problem.

Steps:

  1. Created a new ASP.NET Web Application
  2. Using NuGet got and installed Nancy.Hosting.Aspnet
  3. Checked that the web.config was configured by NuGet and it looks right
  4. added a new class which has this code:

     using Nancy; namespace test2 { public class MainModule : NancyModule { Get["/"] = parameters => "Hello World"; } } 

    Result: Get["/"] = parameters => "Hello World"; doesn't compile

You need to put your route declaration inside the constructor of the MainModule. Putting the declaration straight into the body, of the class, in not a valid C# syntax.

As TheCodeJunkie mentions, Get["/"] = parameters => "Hello World"; needs to be placed in a constructor for the class.

using Nancy;

namespace test2
{
    public class MainModule : NancyModule
    {
        public MainModule() {
            Get["/"] = parameters => "Hello World";
        }
    }
}

除上述内容外,请注意,最新版本的语法已更改,定义现在看起来像

Get("/", parameters => "Hello World");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM