Session is a method to save the session state of users and web applications. ASP.NET Core provides a middleware for managing session state. This article mainly introduces the use of Session in Asp.net Core. If you are interested You can find out more about it,
Preface
2017 started quietly, and 2017 is another particularly important year for me.
I wrote an Asp.net Core at home during the New Year's Day holidayVerification codeLog in. I encountered two small problems during the demo process. The first was to reference the dll in Asp.net Core. In the past, we referenced DLL directly. This is not possible in Core. It must be added based on NuGet, or added based on project.json, and then saving the VS will start to restore the class library.
The second is the problem of using Session. To use Session in Core, you need to add the Session class library.
Add Session
Add based on NuGet on your project: Microsoft.AspNetCore.Session.
Modify startup.cs
Find the method ConfigureServices(IServiceCollection services) in startup.cs and inject Session (this place is Asp.net Core pipeline):services.AddSession();
Next we need to tell Asp.net Core to use memory to store Session data and add code in Configure(IApplicationBuilder app,...): app.UserSession( );
Session
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Controller. 2. If it is not in Controller , you can inject IHttpContextAccessor
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Storage complex objects
Serialize the object into a json when storing the object StringStorage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
1 2 3 4 5 |
|
Use SQL Server or RedisStorage
1, SQL ServerAdd reference "Microsoft.Extensions.Caching. SqlServer": "1.0.0"Injection:1 2 3 4 5 6 7 8 |
|
1 2 3 |
|
Li Yanhui ASP basic video tutorial
The above is the detailed content of Analyze how Asp.net uses Session. For more information, please follow other related articles on the PHP Chinese website!