C#利用ASP.NET?Core開發(fā)學(xué)生管理系統(tǒng)詳解
隨著技術(shù)的進(jìn)步,跨平臺(tái)開發(fā)已經(jīng)成為了標(biāo)配,在此大背景下,ASP.NET Core也應(yīng)運(yùn)而生。本文主要利用ASP.NET Core開發(fā)一個(gè)學(xué)生管理系統(tǒng)為例,簡述ASP.NET Core開發(fā)的常見知識(shí)點(diǎn),僅供學(xué)習(xí)分享使用,如有不足之處,還請指正。
涉及知識(shí)點(diǎn)
開發(fā)學(xué)生管理系統(tǒng),涉及知識(shí)點(diǎn),如下所示:
開發(fā)工具:Visual Studio 2019
目標(biāo)框架:.Net 5.0
架構(gòu):MVC三層架構(gòu)【Model-View-Controller】
創(chuàng)建項(xiàng)目
文件-->新建-->項(xiàng)目-->ASP.NET Core Web應(yīng)用(模型-視圖-控制器),如下所示:

然后點(diǎn)擊下一步,進(jìn)入配置新項(xiàng)目頁面,輸入項(xiàng)目名稱【SMS=Student Management System】及保存位置,然后點(diǎn)擊下一步,如下所示:

選擇其他信息【目標(biāo)框架選擇.NET 5.0】,然后點(diǎn)擊創(chuàng)建,如下所示:

通過默認(rèn)創(chuàng)建的項(xiàng)目,如下所示:

登錄模塊
1. 創(chuàng)建控制器--LoginController
在Controllers文件夾-->右鍵添加-->控制器,如下所示:

打開創(chuàng)建視圖控制器窗口,選擇MVC控制器-空,然后點(diǎn)擊添加。 如下所示:

彈出添加新項(xiàng)窗口,選擇MVC控制器-空,輸入控制器名稱,點(diǎn)擊創(chuàng)建即可,如下所示:

控制器代碼如下所示:
namespace SMS.Controllers
{
public class LoginController : Controller
{
private DataContext dataContext;
public LoginController(DataContext context) {
dataContext = context;
}
[HttpGet]
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Login(User user)
{
if (string.IsNullOrEmpty(user.UserName) || string.IsNullOrEmpty(user.Password))
{
ViewBag.Msg = "用戶名或密碼為空";
return View("Index", user);
}
else {
var item = dataContext.Users.FirstOrDefault(i=>i.UserName==user.UserName && i.Password == user.Password);
if (item != null)
{
HttpContext.Session.SetInt32("UserId",item.Id);
return Redirect("/Home");
}
else
{
ViewBag.Msg = "用戶名或密碼驗(yàn)證錯(cuò)誤";
return View("Index", user);
}
}
}
}
}2. 創(chuàng)建登錄視圖
在Views文件夾下新增Login文件夾,然后新增視圖【Index.cshtml】,如下所示:

然后選擇空視圖,如下所示:

輸入視圖名稱【Index.cshtml】,點(diǎn)擊添加即可,如下所示:

登錄頁面,添加如下代碼,如下所示:
<!DOCTYPE html>
<html>
<head>
<title>學(xué)生管理系統(tǒng)</title>
<link rel="stylesheet" href="/css/login.css" rel="external nofollow" >
<!-- For-Mobile-Apps-and-Meta-Tags -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- //For-Mobile-Apps-and-Meta-Tags -->
</head>
<body>
<h1>學(xué)生管理系統(tǒng)</h1>
<div class="container w3">
<form action="/Login/Login" method="post">
<div class="username">
<span class="username">Username:</span>
<input type="text" id="UserName" name="UserName" class="name" placeholder="" required="">
<div class="clear"></div>
</div>
<div class="password-agileits">
<span class="username">Password:</span>
<input type="password" id="Password" name="Password" class="password" placeholder="" required="">
<div class="clear"></div>
</div>
<div class="rem-for-agile">
<input type="checkbox" name="remember" class="remember">記住密碼<br>
</div>
<div class="login-w3">
<input type="submit" class="login" value="登 錄">
</div>
<div class="clear"></div>
<div style="color:red;font-size:13px;">
@ViewBag.Msg
</div>
</form>
</div>
<div class="footer-w3l">
<p> ? 2021 學(xué)生管理系統(tǒng). All Rights Reserved | Design by 小六公子</p>
</div>
</body>
</html>3. 創(chuàng)建用戶模型
在Models文件夾下,右鍵添加類,如下所示:

輸入模型名稱【User】,點(diǎn)擊添加即可,如下所示:

用戶模型User,如下所示:
namespace SMS.Models
{
public class User
{
/// <summary>
/// 用戶唯一標(biāo)識(shí)
/// </summary>
public int Id { get; set; }
/// <summary>
/// 登錄賬號(hào)
/// </summary>
public string UserName { get; set; }
/// <summary>
/// 密碼
/// </summary>
public string Password { get; set; }
/// <summary>
/// 顯示名稱
/// </summary>
public string NickName { get; set; }
}
}4. 創(chuàng)建數(shù)據(jù)庫操作DataContext
數(shù)據(jù)庫操作采用EntityFrameCore框架,繼承自DbContext,如下所示:
namespace SMS.Models
{
public class DataContext:DbContext
{
public DbSet<User> Users { get; set; }
public DataContext(DbContextOptions options) : base(options)
{
}
}
}5. 創(chuàng)建數(shù)據(jù)庫和表并構(gòu)造數(shù)據(jù)
創(chuàng)建數(shù)據(jù)庫和表并構(gòu)造數(shù)據(jù),如下所示:

6. 添加數(shù)據(jù)庫連接配置
連接數(shù)據(jù)庫,需要在配置文件appsettings.json中,添加數(shù)據(jù)庫連接字符串,如下所示:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"ConnectionStrings": {
"Default": "Server=localhost;Database=SMS;Trusted_Connection=True;User Id=sa;Password=abc123"
},
"AllowedHosts": "*"
}7. 添加注入信息
在Startup.cs中,添加EntittyFramework的注入,如下所示:
namespace SMS
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
//數(shù)據(jù)庫EntityFrameworkCore注入
services.AddDbContext<DataContext>(options=>options.UseSqlServer(Configuration.GetConnectionString("Default")));
services.AddHttpContextAccessor();
services.AddSession();//配置session訪問服務(wù)
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseSession();//需是注入session
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}8. 運(yùn)行測試
經(jīng)過以上步驟,登錄功能已經(jīng)做好,運(yùn)行程序。然后數(shù)據(jù)賬號(hào)密碼,點(diǎn)擊登錄進(jìn)行跳轉(zhuǎn),如下所示:

以上就是C#利用ASP.NET Core開發(fā)學(xué)生管理系統(tǒng)詳解的詳細(xì)內(nèi)容,更多關(guān)于C# ASP.NET Core學(xué)生管理系統(tǒng)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#?EF?Core可視化工具的使用及EF?Core入門語句操作代碼
EF?Core?可用作對(duì)象關(guān)系映射程序?(O/RM),以便于?.NET?開發(fā)人員能夠使用?.NET?對(duì)象來處理數(shù)據(jù)庫,這樣就不必經(jīng)常編寫大部分?jǐn)?shù)據(jù)訪問代碼了,接下來通過本文給大家介紹C#?EF?Core可視化工具的使用及EF?Core入門語句,感興趣的朋友一起看看吧2022-02-02
C# 調(diào)用API函數(shù)彈出映射網(wǎng)絡(luò)驅(qū)動(dòng)器對(duì)話框問題
C#中的.net的常用對(duì)話框中沒有映射網(wǎng)絡(luò)驅(qū)動(dòng)映射對(duì)話框,所以需要用windows的API函數(shù)去實(shí)現(xiàn)彈出映射網(wǎng)絡(luò)驅(qū)動(dòng)器對(duì)話框2014-01-01
C#實(shí)現(xiàn)航班預(yù)訂系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)航班預(yù)訂系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
C#實(shí)現(xiàn)簡單點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡單點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C# Winform實(shí)現(xiàn)波浪滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了C# Winform實(shí)現(xiàn)波浪滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
c# 服務(wù)器上傳木馬監(jiān)控代碼(包含可疑文件)
c# 監(jiān)控服務(wù)器上傳木馬(包含可疑文件)2010-05-05
C#如何提取經(jīng)緯度文件中的經(jīng)緯度數(shù)據(jù)
近期開發(fā)時(shí)需要獲取當(dāng)前的經(jīng)緯度坐標(biāo),下面這篇文章主要給大家介紹了關(guān)于C#如何提取經(jīng)緯度文件中經(jīng)緯度數(shù)據(jù)的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08

