Objective
Last November to participate in the Qingdao MVP line activities, will be the old MVP Mingzhi introduced Nancy, has not been a systematic study, and recently just empty, combined with. NET Core learning summed up a bit.
Note: Most of the content in this article is the translation and summary of official website documents.
Brief introduction
Nancy is a lightweight HTTP service framework, full name NANCYFX, Nancy framework inspired by Ruby
Sinatra Framework, whose author's name is Frank Sinatra, nancyfx the name of Nancy is the name of Frank Sinatra daughter, FX means the framework, that is, the frame.
Nancy's Advantages
- easy to test (easier testing), Nancy provides a test library that makes it easy to test for request response
- Automatic Dependency Injection (Automatic Dependency Resolution), Nancy uses TINYIOC as a dependency injection framework
- content Negotiation, Nancy can use both to create API apps, to create site apps, and even to mix.
- Simple Syntax (terse Syntax & less ceremony), syntax is much simpler than MVC
- no configuration (no config), no configuration required for Nancy to start and run
- can be run anywhere (Runs anywhere), Nancy can host in IIS, WCF, executable exe files, in Windows service or in a self-hosted app (Hosted application)
- Pipe hooks (Pipeline Hooks), Nancy allows the user to modify the pipeline to customize some logic before the request is processed and after the response is sent
Writing the first Nancy application
First we create an empty. Net Core App, named Hellonancy
After the project is successfully created, use NuGet to introduce the Nancy Library, select the (2.0.0-barneyrubble) version
Nancy 2.0.0-barneyrubble is a. NET Stardard-based project, so you can use it in. NET Core
Note: When writing this instance code, I am using. NET core 2.1.4, which already contains Microsoft.AspNetCore.Owin in Microsoft.AspNetCore.All, if your. NET core version is low, Adding Microsoft.AspNetCore.Owin libraries manually with NuGet
Once the reference has been added, we'll modify Startup.cs, where we can use the app.UseOwin
method to register Nancy in the pipeline of net Core MVC
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseOwin(x => x.UseNancy()); }
Next we add a new class Hellomodule, HelloModule
inherited fromNancyModule
public class HelloModule : NancyModule { public HelloModule() { Get("/", p => "Hello World"); } }
There are 2 parameters in the Get method, the first parameter is a route template similar to the net core MVC route, and the second parameter is the response when the URL requested by the user conforms to the route template, equivalent to the action in net core MVC.
Now that one of the simplest Nancy applications has been completed, the following we launch the project, the website correctly shows "Hello World"
What, it seems to be not very concise.
Attached source code
Lamond Lu
Source: www.cnblogs.com/lwqlun/p/9532803.html
This site uses the "Attribution 4.0 International" Creative sharing agreement, please indicate the author and source in the obvious position of the article.