Using sessions is troubled.
This commit is contained in:
@@ -5,6 +5,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using WebPostPartialJsonObject.Extensions;
|
||||
using WebPostPartialJsonObject.Models;
|
||||
|
||||
namespace WebPostPartialJsonObject.Controllers
|
||||
@@ -25,7 +26,8 @@ namespace WebPostPartialJsonObject.Controllers
|
||||
[HttpPost, Consumes("application/json")]
|
||||
public IActionResult Index([FromBody] ExpectedViewModel data)
|
||||
{
|
||||
return View();
|
||||
HttpContext.Session.SetObject("KioskSettings", data);
|
||||
return RedirectToAction("Index", "Settings");
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
|
||||
15
WebPostPartialJsonObject/Controllers/SettingsController.cs
Normal file
15
WebPostPartialJsonObject/Controllers/SettingsController.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WebPostPartialJsonObject.Extensions;
|
||||
using WebPostPartialJsonObject.Models;
|
||||
|
||||
namespace WebPostPartialJsonObject.Controllers
|
||||
{
|
||||
public class SettingsController : Controller
|
||||
{
|
||||
public IActionResult Index()
|
||||
{
|
||||
ExpectedViewModel data = HttpContext.Session.GetObject<ExpectedViewModel>("KioskSettings");
|
||||
return View(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
19
WebPostPartialJsonObject/Extensions/SessionExtensions.cs
Normal file
19
WebPostPartialJsonObject/Extensions/SessionExtensions.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Newtonsoft.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace WebPostPartialJsonObject.Extensions
|
||||
{
|
||||
public static class SessionExtensions
|
||||
{
|
||||
public static void SetObject(this ISession session, string key, object value)
|
||||
{
|
||||
session.SetString(key, JsonConvert.SerializeObject(value));
|
||||
}
|
||||
public static T GetObject<T>(this ISession session, string key)
|
||||
{
|
||||
var value = session.GetString(key);
|
||||
return value == null ? default : JsonConvert.DeserializeObject<T>(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,8 +22,12 @@ namespace WebPostPartialJsonObject
|
||||
// This method gets called by the runtime. Use this method to add services to the container.
|
||||
public void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
services.AddControllersWithViews();
|
||||
}
|
||||
services.AddSession(options =>
|
||||
{
|
||||
options.IdleTimeout = TimeSpan.FromHours(2.0);
|
||||
});
|
||||
services.AddControllersWithViews().AddSessionStateTempDataProvider();
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
@@ -37,6 +41,7 @@ namespace WebPostPartialJsonObject
|
||||
app.UseExceptionHandler("/Home/Error");
|
||||
}
|
||||
app.UseStaticFiles();
|
||||
app.UseSession();
|
||||
|
||||
app.UseRouting();
|
||||
|
||||
|
||||
38
WebPostPartialJsonObject/Views/Settings/Index.cshtml
Normal file
38
WebPostPartialJsonObject/Views/Settings/Index.cshtml
Normal file
@@ -0,0 +1,38 @@
|
||||
@model WebPostPartialJsonObject.Models.ExpectedViewModel
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Index";
|
||||
}
|
||||
|
||||
<h1>Index</h1>
|
||||
|
||||
<h4>ExpectedViewModel</h4>
|
||||
<hr />
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<form asp-action="Index">
|
||||
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
|
||||
<div class="form-group">
|
||||
<label asp-for="KioskName" class="control-label"></label>
|
||||
<input asp-for="KioskName" class="form-control" />
|
||||
<span asp-validation-for="KioskName" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="StartUp" class="control-label"></label>
|
||||
<input asp-for="StartUp" class="form-control" />
|
||||
<span asp-validation-for="StartUp" class="text-danger"></span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<input type="submit" value="Save" class="btn btn-primary" />
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<a asp-action="Index">Back to List</a>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
|
||||
}
|
||||
@@ -4,4 +4,9 @@
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.5" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user