For VS2017 follow this official MSDN article regarding Session and Application Status in ASP.NET Core . You can test your script in the following example that I created. Note Although the code below looks long, you are actually only making minor changes to the default application, which is created from the main ASP.NET template template. Just follow these steps:
Build an ASP.NET Core MVC Application Using the VS2017 Default VS2017
Change the default value of the Home controller as shown below
Make sure the Startup.cs file has session related entries, as shown in the Startup.cs file below
Launch the application and click the Home link on the top panel. This will store the session values โโshown below ( Nam Wam , 2017 and current date )
Click the About link on the top panel. You will notice that session values โโhave been passed to the About controller. But I know that was not your question, since this is only a test of passing a session of value for another action on the same controller. So, to answer your question, follow the next 3 steps.
Create another AnotherController controller - as shown below - with the new Test() action and View Test.cshtml inside the Views\Test folder
In _Layout.cshtml add another link <li><a asp-area="" asp-controller="Another" asp-action="Test">Test View</a></li> right after <li>...Contact...</li> as shown below
Launch the application again and first click the Home link at the top of the bar. Then click the Test link in the top pane. You will notice that the session values โโwere passed from HomController to AnotherController and were successfully displayed in the Test view.
Homecontroller
using System; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using Newtonsoft.Json; namespace MyProject.Controllers { public class HomeController : Controller { const string SessionKeyName = "_Name"; const string SessionKeyFY = "_FY"; const string SessionKeyDate = "_Date"; public IActionResult Index() { HttpContext.Session.SetString(SessionKeyName, "Nam Wam"); HttpContext.Session.SetInt32(SessionKeyFY , 2017);
About.cshtml [Displaying session variable values โโfrom the same controller]
@{ ViewData["Title"] = "ASP.Net Core !!"; } <h2>@ViewData["Title"].</h2> <h3>@ViewData["Message"]</h3> <table class="table table-responsive"> <tr> <th>Name</th> <th>Fiscal Year</th> </tr> <tr> <td>@ViewBag.Name</td> <td>@ViewBag.FY</td> </tr> </table> <label>Date : @(ViewBag.Date.ToString("dd/MM/yyyy") != "01/01/0001" ? ViewBag.Date.ToString("dd/MM/yyyy") : "")</label>
AnotherController . [Other controller than HomeController]:
using System; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; public class AnotherController : Controller { const string SessionKeyName = "_Name"; const string SessionKeyFY = "_FY"; const string SessionKeyDate = "_Date";
Test.cshtml : [Mapping session variable values โโpassed from the Home controller to Another controller]
@{ ViewData["Title"] = "View sent from AnotherController"; } <h2>@ViewData["Title"].</h2> <h3>@ViewData["Message"]</h3> <table class="table table-responsive"> <tr> <th>Test-Name</th> <th>Test-FY</th> </tr> <tr> <td>@ViewBag.Name</td> <td>@ViewBag.FY</td> </tr> </table> <label>Date : @(ViewBag.Date.ToString("dd/MM/yyyy") != "01/01/0001" ? ViewBag.Date.ToString("dd/MM/yyyy") : "")</label>
_Layout.cshtml
.... <div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li> <li><a asp-area="" asp-controller="Home" asp-action="About">About</a></li> <li><a asp-area="" asp-controller="Home" asp-action="Contact">Contact</a></li> <li><a asp-area="" asp-controller="Another" asp-action="Test">Test View</a></li> </ul> </div> ....
Startup.cs : [Make sure it includes some session-related entries. Most likely, when you created the ASP.NET Core MVC application in VS2017, these entries will already be there. But just make sure.]
.... // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //In-Memory services.AddDistributedMemoryCache(); services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(1);//Session Timeout. }); // Add framework services. services.AddMvc(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } ....