Getting to know .Net Core Mvc (session login + query)

A brief introduction to ASP.NET Core MVC:

Controllers: Controller folder

Views: View folder

wwwroot: static file folder, including css, js, img, etc.

Program.cs: application entry file, including Main function, used to configure and execute application

Startup.cs: startup file, used to store application startup and configuration code

demo.csproj: application configuration file

Other files: mainly to configure front-end compression and obtain files for class libraries

 

The first step is to create the project

 

The second step is to connect to the database

Scaffold-DbContext "Server=.;Database=users;Uid=sa;Pwd=123456;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Generate entity classes

The third step, we start to do a simple login + home page query

No.1 Create a login controller (Login, (I see my habit here, I prefer orderly)

 

NO.2 Design a simple login page (feel free, naughty)

 Start writing code. . . . . The form submission is used here, which is convenient and quick

 

Next, talk about the usage of session

Session, you need to add support for Session, otherwise it will report an error Session has not been configured for this application or request 

a) nuget installs Microsoft.AseNetCore.Session ( if there is no need to install it, it is usually available by default );


b) ConfigureServices中services.AddSession ();


c) Configure中app.UseSession();


d) TempData depends on Session, so Session must be configured.
e) HttpContext. Session, but the original only has two methods: void Set(string key, byte [] value) and boolTryGetValue(string key, out byte [] value). If you use Microsoft AspNetCore.Http; (you need to install Microsoft AspNetCore.Http. Extensions ), you can also use Session Extensions whose values ​​are int and string types, and other types can only use json for serialization.
f) It is recommended to use redis as a process to process session

Login is done, now look at the home page

 Controller code

 

Show results 

 Successful login jump to homepage

This is just a simple example. When you are simply practicing, you can do it yourself. Be rigorous . How can the password be seen by others?

 

Little Baiyuan who is studying silently, when the boss is passing by, please give pointers. . . . . . .

Guess you like

Origin blog.csdn.net/weixin_48175309/article/details/109291829