一.首先我先說說.net core與.net farmwork 的區別(自己的見解):
1..net farmwork 它只能在windows系統上搭建項目,並且微軟也沒對它開源,
2.就是.net core可以跨平台 支持Windows,Linux和Mac OS平台開發
3.未來即.net core技術,.net core技術有較好的發展前景
總結:目前而言,.Net Framework是非常方便的平台,.Net Core的方便度尚不如.Net Framework。但是.Net Core的免費和跨平台特性,滿足了很多小型面向業務型開發的公司的需求,是非常好的Java的替代品。
二:接下來就來創建一個屬於自己的第一個.net core項目
1.首先找到我們要創建的項目(注意是c#語言,.net core項目):
2.點擊下一步
3.然后點擊創建(注意:版本號是2.1版本,平台是.net core開發平台,創建的是模型視圖控制器)
4.再次點擊創建,一個項目就創建完成了,至於創建項目下的文件及它的作用,我在這里就不一一介紹了,可以自行百度,創建好的項目如下圖所示:
5.在models文件夾下創建一個類,Content類,代碼如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 6 namespace WebApplication5.Models 7 { 8 public class Content 9 { 10 /// <summary> 11 /// 主鍵 12 /// </summary> 13 public int Id { get; set; } 14 15 /// <summary> 16 /// 標題 17 /// </summary> 18 public string title { get; set; } 19 /// <summary> 20 /// 內容 21 /// </summary> 22 public string content { get; set; } 23 /// <summary> 24 /// 狀態 1正常 0刪除 25 /// </summary> 26 public int status { get; set; } 27 /// <summary> 28 /// 創建時間 29 /// </summary> 30 public DateTime add_time { get; set; } 31 /// <summary> 32 /// 修改時間 33 /// </summary> 34 public DateTime modify_time { get; set; } 35 } 36 }
6.在控制器文件夾下創建一個控制器,名稱叫:ContentController,在這里添加控制器的代碼如下:

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Threading.Tasks; 5 using Microsoft.AspNetCore.Mvc; 6 using WebApplication5.Models; 7 8 namespace WebApplication5.Controllers 9 { 10 public class ContentController : Controller 11 { 12 /// <summary> 13 /// 首頁顯示 14 /// </summary> 15 /// <returns></returns> 16 public IActionResult Index() 17 { 18 var contents = new List<Content>(); 19 for (int i = 1; i < 11; i++) 20 { 21 contents.Add(new Content { Id = i, title = $"{i}的標題", content = $"{i}的內容", status = 1, add_time = DateTime.Now.AddDays(-i) }); 22 } 23 return View(new ContentViewModel { Contents = contents }); 24 } 25 } 26 }
7.創建視圖,視圖創建好之后,添加如下代碼:

1 @{ 2 ViewData["Title"] = "Index"; 3 } 4 5 <h2>Index</h2> 6 <div class="panel panel-default todo-panel"> 7 <div class="panel-heading"></div> 8 9 <table class="table table-hover"> 10 <thead> 11 <tr> 12 <td> <input type="checkbox" class="done-checkbox"></td> 13 <td>序號</td> 14 <td>標題</td> 15 <td>內容</td> 16 <td>添加時間</td> 17 </tr> 18 </thead> 19 20 @foreach (var item in Model.Contents) 21 { 22 <tr> 23 <td> 24 <input type="checkbox" class="done-checkbox"> 25 </td> 26 <td>@item.Id</td> 27 <td>@item.title</td> 28 <td>@item.content</td> 29 <td>@item.add_time</td> 30 31 </tr> 32 } 33 </table> 34 </div>
8.以為這樣就完成了嗎?不,還沒有,在view文件夾下的Shared的_Layout.cshtml頁面導航欄下添加如下代碼:
<li><a asp-area="" asp-controller="Content" asp-action="Index">Content</a></li>
這行代碼作用是為了我們的頁面添加一個導航欄的選擇項,顯示出我們所要的效果,更直觀的觀看我們要實現的效果
9.完成以上步驟之后,你的第一個.net core web程序就創建好了,點擊啟動項目即可,出來的效果如下:
10.所有的步驟完成后,是不是以為你的效果就能實現了,中間添加視圖時需要安裝一個NuGet包,我這里沒有寫步驟,畢竟代碼這一行,還是需要自己親身實踐才能學得會,找到問題並解決它也是一種能力.
一些代碼我也是剛學習另一個博主的博客寫出來的,原博主鏈接: https://www.cnblogs.com/yilezhu/p/9985451.html
覺得我寫的對你有一些幫助的,請幫忙點點贊,關注一下,我會回關的