1. 程式人生 > >【.NetCore學習】ubuntu16.04 搭建.net core mvc api 運行環境

【.NetCore學習】ubuntu16.04 搭建.net core mvc api 運行環境

xen dir ftp arc mov png 運行環境 reg repos

查看linux內核版本

uname -a

打印結果

python@ubuntu:~$ uname -a
Linux ubuntu 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

技術分享圖片

查看ubuntu系統版本

lsb_release -a

打印結果

python@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 
16.04.1 LTS Release: 16.04 Codename: xenial python@ubuntu:~$

可以看到版本號是 16.4

查看官方文檔執行 dotnet 環境安裝過程

https://www.microsoft.com/net/learn/get-started/linuxredhat

這裏我們選擇 Ubuntu

技術分享圖片

繼續執行下面的命令

Register the trusted Microsoft signature key:

curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg

繼續

sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg

根據系統版本,執行下面的命令

sudo sh -c echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list

好了,環境部署完畢,下面我們安裝 SDK

分別執行下面三行命令

sudo apt-get install apt-transport-https
sudo apt
-get update sudo apt-get install dotnet-sdk-2.1.4

技術分享圖片

至此,SDK安裝完畢

---------------------------------------------------------------------------------------------------------------------

下面我們新建一個控制臺項目測試一下

dotnet new console -o myApp
cd myApp
dotnet run

遇到了一個目錄權限問題

Template "Console Application" could not be created.
Access to the path ‘/etc/apt/trusted.gpg.d/myApp‘ is denied.

技術分享圖片

使用權限命令,添加目錄的權限

然而這裏,我們修改一下創建位置,改成當前的 home 目錄創建即可

技術分享圖片

運行結果如下

python@ubuntu:~/dotnet$ dir
myApp
python@ubuntu:~/dotnet$ cd myApp/
python@ubuntu:~/dotnet/myApp$ dir
myApp.csproj  obj  Program.cs
python@ubuntu:~/dotnet/myApp$ dotnet run
Hello World!
python@ubuntu:~/dotnet/myApp$ 

編譯運行成功了。

下面我們用在 windows 下編譯好的 mvcapi 項目在 linux 上運行一下試試。

首先我們把整個項目解決方案目錄傳到 linux 服務器上

技術分享圖片

這裏我用的是 xftp

技術分享圖片

然後要運行哪個項目,就直接切換到項目的目錄,例如我要運行 mywebapi 這個api接口

python@ubuntu:~/dotnet/myweb$ cd mywebapi
python@ubuntu:~/dotnet/myweb/mywebapi$ ls
appsettings.Development.json  bin          mywebapi.csproj       obj         Properties  wwwroot
appsettings.json              Controllers  mywebapi.csproj.user  Program.cs  Startup.cs
python@ubuntu:~/dotnet/myweb/mywebapi$ dotnet run
Models/GEDU_LSA_V_1_7_1Context.cs(38,10): warning CS1030: #warning: ‘To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.‘ [/home/python/dotnet/myweb/core/mydb.csproj]
Controllers/MyController.cs(24,13): warning CS0162: Unreachable code detected [/home/python/dotnet/myweb/mywebapi/mywebapi.csproj]
Using launch settings from /home/python/dotnet/myweb/mywebapi/Properties/launchSettings.json...
Hosting environment: Development
Content root path: /home/python/dotnet/myweb/mywebapi
Now listening on: http://localhost:61697
Application started. Press Ctrl+C to shut down.
^CApplication is shutting down...

技術分享圖片

這個時候我們訪問以下 api 接口地址

技術分享圖片

成功看到接口返回的json

mvc項目也是這樣操作,即可。

【.NetCore學習】ubuntu16.04 搭建.net core mvc api 運行環境