ASP.NET MVC下实现前端视图页的Session

Haidee ·
更新时间:2024-09-20
· 544 次阅读

  在ASP.NET MVC的控制器中可以实现Session处理。如果要在前端视图页实现Session该如何做呢?可以使用window.sessionStorage来做。 AlexChittock用jQuery做了实现。在这里: https://github.com/AlexChittock/JQuery-Session-Plugin   具体实现很简单: @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>Index</h2> <input type="text" id="guess"/> <br/> <input type="button" id="btn" value="我猜"/> @section scripts { <script src="~/Scripts/jquery.session.js"></script> <script type="text/javascript"> $(function() { //$.session.set('some key', 'a value'); //$.session.get('some key'); //$.session.clear(); //$.session.remove('some key'); $.session.set(mySessionKey, "Hello World"); $('#btn').on("click", function() { if ($('#guess').val() == $.session.get(mySessionKey)) { alert("恭喜你猜对了~~"); } }); }); var mySessionKey = "mykey"; </script> }



net asp.net mvc ASP.NET 视图 MVC 前端 session ASP

需要 登录 后方可回复, 如果你还没有账号请 注册新账号