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

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

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

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

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

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

彈出添加新項窗口,選擇MVC控制器-空,輸入控制器名稱,點擊創(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 = "用戶名或密碼驗證錯誤";
return View("Index", user);
}
}
}
}
}2. 創(chuàng)建登錄視圖
在Views文件夾下新增Login文件夾,然后新增視圖【Index.cshtml】,如下所示:

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

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

登錄頁面,添加如下代碼,如下所示:
<!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】,點擊添加即可,如下所示:

用戶模型User,如下所示:
namespace SMS.Models
{
public class User
{
/// <summary>
/// 用戶唯一標(biāo)識
/// </summary>
public int Id { get; set; }
/// <summary>
/// 登錄賬號
/// </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. 運行測試
經(jīng)過以上步驟,登錄功能已經(jīng)做好,運行程序。然后數(shù)據(jù)賬號密碼,點擊登錄進(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#動態(tài)代碼生成控件后其他事件不能獲取該控件值的解決方法
這篇文章主要給大家介紹了關(guān)于C#動態(tài)代碼生成控件后其他事件不能獲取該控件值的解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實現(xiàn)2013-08-08
C#難點逐個擊破(8):可空類型System.Nullable
null值用來表示數(shù)據(jù)類型未被賦予任何值,它是一種引用類型;void表示沒有類型,或者說是沒有任何值。null與void的區(qū)別可以認(rèn)為void是根本沒有,而null是一個空箱子,里面什么都沒有。2010-02-02
C#中DateTimePicker默認(rèn)值顯示為空的問題
這篇文章主要介紹了C#中DateTimePicker默認(rèn)值顯示為空的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

