[NetCore] web api with asp.net core 2.0 on Linux 開發初體驗

RiCo 技術農場
Aug 23, 2017 · 2 min read

Nancy 是一個open source 輕量Web Framework,基於Http的Web服務而生,且目前也支援.netCore2.0了。

之前我因為要快速建立Http Server而接觸到Nancy,Nancy開發上不只簡單且外掛功能相當豐富,

這次簡單紀錄web api with .net Core2.0和Nancy開發過程。

選擇ASP.NET Core 2.0空白專案

Install-Package Microsoft.AspNetCore.Owin

Install-Package Nancy –Pre

空白的web api專案目錄結構很單純,我下面簡單介紹一些檔案和目錄主要職責

Startup.cs:web api啟動入口

Program.cs:程式組態設定,預設內容比asp.net core 1.0更簡單

Wwwroot:所有靜態檔案(如:js,css,images..等)接存放在此資料夾。

新增一個HomeModule class並繼承NancyModule

public class HomeModule : NancyModule
{
public HomeModule()
{
Get("/", args => "Hello Nancy from Get request");
Post("/", args=> "Hello Nancy from Post request");
Get("/os",args=> System.Runtime.InteropServices.RuntimeInformation.OSDescription);
}
}

Publish then copy all files to container

docker cp D:\riconetcore\WebAPIwithNancy\. 858775108f6e:/docker-netcoreapp/WebAPIwithNancy/.

Start http server on Linux

Cd WebAPIwithNancy dotnet WebAPIwithNancy.dll
curl -i http://localhost:5000/
curl -i http://localhost:5000/os

看一下http server輸出資訊

參考

samples

Exploring a minimal WebAPI with .NET Core and NancyFX

Set up a hosting environment for ASP.NET Core on Linux with Nginx, and deploy to it

Create a Web API with ASP.NET Core MVC and Visual Studio Code on Linux, macOS, and Windows

SELF-HOSTING A .NET API: CHOOSING BETWEEN OWIN WITH ASP.NET WEB API AND ASP.NET CORE MVC 1.0

Getting Started With NancyFX In ASP.net Core

Create a web API with ASP.NET Core MVC and Visual Studio for Windows

ASP.NET Core 2.0 Configuration and Razor Pages

Create a Web API with ASP.NET Core MVC and Visual Studio for Mac

Docker Change Port Mapping for an Existing Container

Running NancyFx in a Docker container, a beginner’s guide to build and run .NET applications in Docker

Originally published at dotblogs.com.tw on August 23, 2017.

RiCo’s Note

C#/SQL Server/Azure/NetCore/Angular/Kafka….

)
RiCo 技術農場

Written by

分享工作上實戰經驗,如SQL Server,NetCore,C#,WEBApi,Kafka,Azure…等,Architect,Software Engineer, Technical Manger,Writer and Speaker

RiCo’s Note

C#/SQL Server/Azure/NetCore/Angular/Kafka….

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade