Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to develop Student Management system with ASP.NET Core in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the "C#how to use ASP.NET Core to develop student management system" knowledge, in the actual case of the operation process, many people will encounter such a dilemma, then let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Knowledge points involved

Develop a student management system involving knowledge points as follows:

Development Tools: Visual Studio 2019

Goal framework: . Net 5.0

Architecture: MVC three-tier architecture [Model-View-Controller]

create a project

File--> New--> Project-->ASP.NET Core Web App (Model-View-Controller), as follows:

Then click Next to enter the new project configuration page, enter the project name [SMS=Student Management System] and save location, and then click Next, as shown below:

选择其他信息【目标框架选择.NET 5.0】,然后点击创建,如下所示:

通过默认创建的项目,如下所示:

登录模块1. 创建控制器--LoginController

在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); } } } }}2. 创建登录视图

在Views文件夹下新增Login文件夹,然后新增视图【Index.cshtml】,如下所示:

然后选择空视图,如下所示:

输入视图名称【Index.cshtml】,点击添加即可,如下所示:

登录页面,添加如下代码,如下所示:

学生管理系统 学生管理系统 Username: Password: 记住密码

@ViewBag.Msg

© 2021 学生管理系统. All Rights Reserved | Design by 小六公子

3. 创建用户模型

在Models文件夹下,右键添加类,如下所示:

输入模型名称【User】,点击添加即可,如下所示:

用户模型User,如下所示:

namespace SMS.Models{ public class User { /// /// 用户唯一标识 /// public int Id { get; set; } /// /// 登录账号 /// public string UserName { get; set; } /// /// 密码 /// public string Password { get; set; } /// /// 显示名称 /// public string NickName { get; set; } }}4. 创建数据库操作DataContext

数据库操作采用EntityFrameCore框架,继承自DbContext,如下所示:

namespace SMS.Models{ public class DataContext:DbContext { public DbSet Users { get; set; } public DataContext(DbContextOptions options) : base(options) { } }}5. 创建数据库和表并构造数据

创建数据库和表并构造数据,如下所示:

6. 添加数据库连接配置

连接数据库,需要在配置文件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": "*"}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(); //数据库EntityFrameworkCore注入 services.AddDbContext(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?}"); }); } }}8. 运行测试

经过以上步骤,登录功能已经做好,运行程序。然后数据账号密码,点击登录进行跳转,如下所示:

"C#怎么用ASP.NET Core开发学生管理系统"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report