- Instant Nancy Web Development
- Christian Horsdal
- 400字
- 2021-08-04 09:59:26
How it works...
Let's take a quick look at what happened in the previous section. The interesting parts are steps 3 and 4.
In step 3, you installed the Nancy.Hosting.Aspnet
package and the Nancy NuGet
packages into the newly created HelloNancy
project. This will download the Nancy
assembly and the Nancy.Hosting.Aspnet
assembly from the NuGet gallery, and add references to both in the HelloNancy
project. The Nancy assembly is the Nancy framework, and the Nancy.Hosting.Aspnet
assembly is an adapter for running the Nancy application on top of ASP.NET. In Nancy terms, this is called hosting Nancy on ASP.NET. Hosting is the subject of the Separating application and hosting (Advanced) recipe.
In step 4, you created a NancyModule
class. Nancy modules are at the heart of Nancy applications. They are used to define which routes the application accepts and how it handles each one. For instance, the NancyModule
class in step 4 defines one route and a handler for that route. This is done in the following line:
Get["/"] = _ => "Hello Nancy World";
This tells Nancy that an HTTP GET
request to the route "/"
should be handled by the lambda expression on the right-hand side of the =
sign. In other words, each time Nancy receives an HTTP GET "/"
, it calls the following line of code:
_ => "Hello Nancy World";
In the previous line of code, the lambda expression takes one parameter called _
. It doesn't use the parameter for anything but simply returns the Hello Nancy World
string back to Nancy. Since nothing went wrong, Nancy will create an HTTP 200 OK
response with Hello Nancy World
in the body and return it as the response to HTTP GET "/"
.
On a side note, I like to adopt the convention that whenever a parameter in a lambda is not used in the body of the lambda, I call it _
. I like this because visually it makes the parameter almost disappear, so I instantly know that it is not used. On the other hand, if it is used, I will always give it a name. I will be using _
throughout this book for unused parameters.
On start-up, Nancy will find all Nancy modules in all loaded assemblies and instantiate each one. In the case of the module from step 4 in the previous section, the constructor containing the route definition discussed previously is run.
- 精通JavaScript+jQuery:100%動(dòng)態(tài)網(wǎng)頁(yè)設(shè)計(jì)密碼
- 數(shù)據(jù)庫(kù)系統(tǒng)教程(第2版)
- PostgreSQL for Data Architects
- Python入門很簡(jiǎn)單
- Java程序設(shè)計(jì)與計(jì)算思維
- Designing Hyper-V Solutions
- Essential Angular
- 64位匯編語(yǔ)言的編程藝術(shù)
- PHP網(wǎng)絡(luò)編程學(xué)習(xí)筆記
- 人人都是網(wǎng)站分析師:從分析師的視角理解網(wǎng)站和解讀數(shù)據(jù)
- ASP.NET程序設(shè)計(jì)教程
- Visual C++開發(fā)入行真功夫
- C語(yǔ)言程序設(shè)計(jì)簡(jiǎn)明教程:Qt實(shí)戰(zhàn)
- Scala編程實(shí)戰(zhàn)
- 零基礎(chǔ)輕松學(xué)C++:青少年趣味編程(全彩版)