Nancy in. Net Core Learning Notes

Source: Internet
Author: User
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/9593600.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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.