VS Code WebApi系列——1、配置


Knowledge should be Shared in Free.

最近在研究VS code下的webapi,看了很多文檔,還是微軟官方的例子好,不過不太適應國人習慣,所以寫點東西。

直接了當

開發環境系統信息:

os:windows 10 1909 64位;

vs code:1.45.1 system setup

net core sdk:1.1.14(x64)

net core version:3.1.201

安裝與配置:

1)vs code下載安裝

網址:https://code.visualstudio.com/#alt-downloads

選擇:

下載后安裝,一路next(想換安裝路徑的注意一下路徑選擇對話框)

2)net core下載安裝

網址:https://dotnet.microsoft.com/download

選擇:

 

一樣,一路next

3)打開vs code,左側最后一個選項,Extension,安裝以下拓展

C#(必裝)

C# Extensions(必裝)

Visual Studio IntelliCode(必裝)

csharpwebapi(選裝)

Dotnet Core Essentials(選裝)

ASP.NET Core Snippets(選裝)

 

 4)新建包(包就是文件夾),這里選擇了DapWebApi文件夾

 

5)命令生成項目

首先,在vscode中添加工作區

 

 

 6)文件夾上右鍵選擇Open in Terminal

 

7)在vs code下部生成的命令行中輸入如下命令:

dotnet new webapi,靜候片刻,等其運行完畢,即可得到如4)中的項目文檔結構

8)添加實體文件夾Model,並在文件夾內添加實體類,如下圖所示

 

實體代碼:

  

namespace ****.Model
{
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

9)控制器添加PersonControl,用實體list集合代替數據庫請求,代碼如下:

 

using System.Collections.Generic;
using ****.Model;
using Microsoft.AspNetCore.Mvc;

namespace ****.Controllers
{
    [ApiController]
    [Route("api/person")]
    public class PersonController:ControllerBase
    {
        private List<Person> _listPerson=new List<Person>(){
            new Person(){Id=1,Name="小明",Age=12},
            new Person(){Id=2,Name="小紅",Age=13},
            new Person(){Id=3,Name="小強",Age=11},
        };

        [HttpGet]
        public IActionResult Get()
        {
            return new JsonResult(_listPerson);
        }
    }
}
10)編碼完成,左側倒數第二個按鈕,調試,

 

 在彈出的網頁中輸入webapi網址請求:https://localhost:5001/api/person,瀏覽器內應該顯示以下內容

如果以上步驟都正常實現了,那么趕快樓下喝酒擼個串吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM