Download presentation
Presentation is loading. Please wait.
Published byLambert Martin Modified over 8 years ago
1
ASP.NET 5 WHAT HAPPENED TO SESSION AND APPSETTINGS
2
Microsoft.AspNet.Session Microsoft.AspNet.Session is Middleware Middleware forms a pipeline of components between Request and Response The Middleware pipeline is defined in the Configure method of Startup.cs The services need to be told to use Session in ConfigureServices in Startup.cs Microsoft.Asp.NET is installed using a NuGet package It appears in the dependencies section on project.json
3
Session in ASP.NET 5 Session needs to be called in a controller through HttpContext and has three pairs of methods: Set, SetString, SetInt32, Get, GetString, GetInt32 Set can only accept a binary array as a parameter This is a bit of a problem Applications running on DNX (.NET Execution Environment) can target the.NET Framework or.NET Core. By default both are targeted.NET Core is a scaled down version of the.NET Framework designed to run on any platform However it does not contain System.Runtime.Serialization.Formatters.Binary
4
Using SetString and Json for Session My solution was to create a generic class for Json Serialization using Newtonsoft.Json In my demo, I use Session to hold a generic list of shopping cart items Adding to Session becomes a matter of var json = BusinessLogic.SerializationLogic >.Serialize(cartItems); HttpContext.Session.SetString("cartItems", json); Retrieving from Session is var json = HttpContext.Session.GetString("cartItems"); return BusinessLogic.SerializationLogic >.Deserialize(json);
5
AppSettings and Dependency Injection In ASP.NET 5, controllers allow dependency injection through their constructors First we need to create a class, call it AppSettings to hold the settings A generic.NET class, IOptions will be used to inject my settings into a Settings controller This is the sole purpose of IOptions private IOptions _appSettings; public SettingsController(IOptions appSettings) { _appSettings = appSettings; }
6
Where do I specify the AppSettings? Your AppSettings need to be specified in appsettings.json "AppSettings": { "Message": "Hello KC.NET User Group" }, ConfigureServices in Startup.cs needs to have to have the information to use this section: services.Configure (Configuration.GetSection("AppSettings"));
7
Resources Demo App on GitHub: https://github.com/carolynlschroeder/DemoApp https://github.com/carolynlschroeder/DemoApp PowerPoint presentation: http://schroederconsulting.azurewebsites.net http://schroederconsulting.azurewebsites.net Blog: http://carolynlschroederblog.com/category/homehttp://carolynlschroederblog.com/category/home
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.