[Nancy on. Net Core Docker] Lightweight web framework

Source: Internet
Author: User
Tags dotnet

. NET core is now a big development, although I am now engaged in Python development, but has been concerned about the development of. NET, in the blog park, when we find that everyone will mention the framework of Nancy, after simple use, found that it is so simple and elegant

public class samplemodule:nancy.nancymodule{public    samplemodule ()    {        get["/"] = _ = "Hello world!";    }}

The code has been uploaded to git, including source code and docker packaging scripts, and you are interested in self-study

Https://github.com/BruceDone/webapi

This is similar to the flask in Python and is easy to use. Today we're going to make a very easy-to-use. NET Core version of the Nancy app, then package the program into Docker and publish and use it.

    • Development environment: Marc Pro
    • Ide:vscode
    • . Net Core
    • Docker

Before the development, please ensure that the above environment has been installed

    1. Create a folder Webapi execute a command
dotnet New

2. After the implementation will be the first to make some necessary documents, open the Package.json, the following content copied into

{  "version":"1.0.0-*",  "buildoptions": {    "Debugtype":"Portable",    "Emitentrypoint": true},"Dependencies": {    "Microsoft.NETCore.App": {      "version":"1.0.0",      "type":"Platform"    },    "Microsoft.AspNetCore.Server.Kestrel":"1.0.0",    "Microsoft.AspNetCore.Owin":"1.0.0",    "Nancy":"2.0.0-barneyrubble"  },  "commands": {    "Web":"Microsoft.AspNet.Server.Kestrel"  },  "Frameworks": {    "netcoreapp1.0": {}  }}

3. OK. Vscode will automatically import the relevant DLL file when it is saved, we'll create the StartUp.cs file after the successful introduction.

using Microsoft.AspNetCore.Builder; using Nancy.owin; namespace nancyapplication{    publicclass  Startup    {        public void Configure (Iapplicationbuilder app)        {            = = x.usenancy ())        ;     }}}

4. Create a HomeModel.cs file after saving

usingNancy;namespacenancyapplication{ Public classHomemodule:nancymodule { PublicHomemodule () {Get ("/", args ="Hello World, it's Nancy on. NET Core"); }    }     Public classPagemodule:nancymodule { PublicPagemodule () {Get ("/person/{name}", args =NewPerson () {Name =args.name}); }    }     Public classPerson { Public stringName {Get;Set; } }}

Routing and Return values have been written, I do not explain the relevant code here, interested in the self-knowledge can be

4. Edit the Program.cs file

usingSystem.IO;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Hosting;namespacenancyapplication{ Public classProgram { Public Static voidMain (string[] args) {            varHost =NewWebhostbuilder (). Usecontentroot (Directory.GetCurrentDirectory ()). Usekestrel (). Useurls ("http://*:5000")                . Usestartup<Startup>()                .             Build (); Host.        Run (); }    }}

5. OK, the necessary files are ready, then run the dotnet run, access http://127.0.0.1:5000 to see the results,

6. Using Docker to deploy programs

To access our API address:

±|master?:1 ? | →curl http://127.0.0.1:5000/person/bruce{"name":"  Bruce"}

Program and code I have been packaged into git, there are related instructions, the program is easily packaged into Docker, so it can be easily clustered or deployed.

Git address: Https://github.com/BruceDone/webapi

If you feel that it is helpful to you, please do not skimp on your star, thank you:)

[Nancy on. Net Core Docker] Lightweight web framework

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.