ASP.NET Core快速入門(第1章:介紹與引入)

 

任務1:介紹

  • 1、介紹與引入

  • 2、配置管理

  • 3、依賴注入

  • 4、ASP.NET Core HTTP介紹

  • 5、認證與授權

  • 6、ASP.NET Core MVC

任務2:環境安裝

下載地址:https://dotnet.microsoft.com/download

通過 visualstudio 安裝:菜單欄上點擊工具--獲取工具和功能(T),勾選 .NET CORE

打開 PowerShell(管理員),輸入以下命令驗證是否安裝成功

PS C:\WINDOWS\system32> dotnet

任務3:在控制檯創建ASP.NET Core應用程序

查看模板:

PS C:\WINDOWS\system32> dotnet new --help

新建項目 ASP.NET Core Web App (Model-View-Controller)

PS D:\jessetalk> mkdir aspdotnetcore
PS D:\jessetalk> cd .\aspdotnetcore\
PS D:\jessetalk\aspdotnetcore> dotnet new mvc

啓動項目

PS D:\jessetalk\aspdotnetcore> dotnet run

瀏覽器訪問:https://localhost:5001/

任務4:在VS中創建ASP.NET Core應用程序

在 VS 中通過 IIS Express 啓動

官方推薦在開發和測試的時候通過控制檯啓動,因爲通過控制檯啓動之後,日誌會自動輸出

任務5:部署到IIS

Windows10如何安裝IIS:https://jingyan.baidu.com/article/eb9f7b6d9e73d1869364e8d8.html

下載 ASP.NET Core Module:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/aspnet-core-module?view=aspnetcore-2.2

安裝完成後在 iis 的模塊下面可以找到以下模塊

在 iis 中添加網站,停掉 Default 網站,新建文件夾sites,在sites裏面新建文件夾aspdotnetcoredemo

在應用程序池中修改 .NET CLR 版本,不然無法運行,因爲託管代碼指代碼編譯成IL代碼後在dotnet framework下運行,aspdotnetcore需要修改爲無託管代碼纔可以運行

在控制檯發佈

PS D:\jessetalk\aspdotnetcore> dotnet publish

發佈結果

發佈到指定目錄

PS D:\jessetalk\aspdotnetcore> dotnet publish -o D:\jessetalk\sites\aspdotnetcoredemo

瀏覽發佈的網站

通過 VS 發佈:

清空該目錄下面的內容:D:\jessetalk\sites\aspdotnetcoredemo,刷新瀏覽器

在 VS 中,項目右鍵--發佈,通過文件夾發佈

刷新瀏覽器

任務6:準備CentOS和Nginx環境

下載 vmware workstation

官網:https://www.vmware.com/products/workstation-pro/workstation-pro-evaluation.html

下載 CentOS,選擇 Minimal ISO

官網:https://www.centos.org/download/

第一次啓動可能出現VMware與 Device/Credential Guard 不兼容 :https://jingyan.baidu.com/article/9f63fb916b50e1c8400f0ebf.html

手動安裝組件:

網卡

修改爲 yes

重啓網卡

安裝 ifconfig 命令

得到 ip 地址 192.168.204.128 之後可以通過 putty 連接(SSH方式)

putty下載地址:https://putty.org/

使用 putty 連接 cenos 之後可以方便複製粘貼命令

安裝 Nginx:https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7

安裝完成後瀏覽器訪問:192.168.204.128

任務7:在CentOS上安裝.NET Core運行時

cenos 安裝 asp .net core 環境:https://www.asp.net/core/overview/aspnet-vnext

安裝後驗證:

[root@localhost ~]# dotnet
[root@localhost ~]# dotnet --version
[root@localhost ~]# dotnet new --help
[root@localhost ~]# cd /
[root@localhost /]# cd home
[root@localhost home]# mkdir netcore
[root@localhost home]# mkdir helloCore
[root@localhost home]# cd helloCore
[root@localhost helloCore]# dotnet new webapi
[root@localhost helloCore]# dotnet run

通過另一個 putty 連接訪問

[root@localhost ~]# curl http://localhost:5000/api/values

任務8:部署到CentOS

下載 FileZilla :https://filezilla-project.org/

通過 ftp 將之前發佈在 sites 目錄下的文件上傳到 cenos 上的 netcore 文件夾裏

啓動網站

[root@localhost home]# cd netcore
[root@localhost netcore]# ls
appsettings.Development.json  aspdotnetcore.pdb                 web.config
appsettings.json              aspdotnetcore.runtimeconfig.json  wwwroot
aspdotnetcore.deps.json       aspdotnetcore.Views.dll
aspdotnetcore.dll             aspdotnetcore.Views.pdb
[root@localhost netcore]# dotnet aspdotnetcore.dll
Hosting environment: Production
Content root path: /home/netcore
Now listening on: http://localhost:5000
Now listening on: https://localhost:5001
Application started. Press Ctrl+C to shut down.

通過另一個 putty 訪問

[root@localhost ~]# curl http://localhost:5000

使用 nginx 將 80 端口 映射到 5000 端口 下

[root@localhost ~]# cd /etc/nginx
[root@localhost nginx]# ls
conf.d                  koi-utf             scgi_params
default.d               koi-win             scgi_params.default
fastcgi.conf            mime.types          uwsgi_params
fastcgi.conf.default    mime.types.default  uwsgi_params.default
fastcgi_params          nginx.conf          win-utf
fastcgi_params.default  nginx.conf.default
[root@localhost nginx]# vi nginx.conf

註釋掉 nginx.conf 文件中默認 80 的 server

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

nginx.conf 文件中將所有 .conf 文件引入進來

#        include /etc/nginx/default.d/*.conf;

修改 netcore.conf

[root@localhost nginx]# cd conf.d
[root@localhost nginx]# vi netcore.conf

netcore.conf

server {
        listen 80;
        location / {
                proxy_pass http://localhost:5000;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

重啓 nginx

[root@localhost conf.d]# nginx -s reload
[root@localhost conf.d]# systemctl restart nginx

瀏覽器訪問:http://192.168.204.128/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章