您好,登錄后才能下訂單哦!
本篇內容介紹了“C#怎么用ASP.NET Core開發學生管理系統”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
開發學生管理系統,涉及知識點,如下所示:
開發工具:Visual Studio 2019
目標框架:.Net 5.0
架構:MVC三層架構【Model-View-Controller】
文件-->新建-->項目-->ASP.NET Core Web應用(模型-視圖-控制器),如下所示:
然后點擊下一步,進入配置新項目頁面,輸入項目名稱【SMS=Student Management System】及保存位置,然后點擊下一步,如下所示:
選擇其他信息【目標框架選擇.NET 5.0】,然后點擊創建,如下所示:
通過默認創建的項目,如下所示:
在Controllers文件夾-->右鍵添加-->控制器,如下所示:
打開創建視圖控制器窗口,選擇MVC控制器-空,然后點擊添加。 如下所示:
彈出添加新項窗口,選擇MVC控制器-空,輸入控制器名稱,點擊創建即可,如下所示:
控制器代碼如下所示:
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); } } } } }
在Views文件夾下新增Login文件夾,然后新增視圖【Index.cshtml】,如下所示:
然后選擇空視圖,如下所示:
輸入視圖名稱【Index.cshtml】,點擊添加即可,如下所示:
登錄頁面,添加如下代碼,如下所示:
<!DOCTYPE html> <html> <head> <title>學生管理系統</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> <h2>學生管理系統</h2> <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 > @ViewBag.Msg </div> </form> </div> <div class="footer-w3l"> <p> ? 2021 學生管理系統. All Rights Reserved | Design by 小六公子</p> </div> </body> </html>
在Models文件夾下,右鍵添加類,如下所示:
輸入模型名稱【User】,點擊添加即可,如下所示:
用戶模型User,如下所示:
namespace SMS.Models { public class User { /// <summary> /// 用戶唯一標識 /// </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; } } }
數據庫操作采用EntityFrameCore框架,繼承自DbContext,如下所示:
namespace SMS.Models { public class DataContext:DbContext { public DbSet<User> Users { get; set; } public DataContext(DbContextOptions options) : base(options) { } } }
創建數據庫和表并構造數據,如下所示:
連接數據庫,需要在配置文件appsettings.json中,添加數據庫連接字符串,如下所示:
{ "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": "*" }
在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(); //數據庫EntityFrameworkCore注入 services.AddDbContext<DataContext>(options=>options.UseSqlServer(Configuration.GetConnectionString("Default"))); services.AddHttpContextAccessor(); services.AddSession();//配置session訪問服務 } // 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?}"); }); } } }
經過以上步驟,登錄功能已經做好,運行程序。然后數據賬號密碼,點擊登錄進行跳轉,如下所示:
“C#怎么用ASP.NET Core開發學生管理系統”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。