你的第一個ASP.NET Core Web 應用程式
一.首先我先說說.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;Content5 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 }ContentController
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>Index.cshtml
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
覺得我寫的對你有一些幫助的,請幫忙點點贊,關注一下,我會回關的