如何检查MVC应用程序中角度js中是否存在会话 [英] How do I check if a session exists in angular js in an MVC application

查看:62
本文介绍了如何检查MVC应用程序中角度js中是否存在会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家好



我目前正忙于MVC中的应用程序,并且在屏幕上有一个调用后端代码中的ActionResult的搜索方法,一切正常我选择的问题是,如果用户离开页面并且会话超时并且用户尝试搜索,因为搜索没有进行实际的回发,它认为它没有拿起会话已经过期,因此Javascript运行并显示搜索动画说它正在搜索,但页面上没有发生任何事情,并且因为会话已经过期而没有调用actionresult。



我怎么能得到Javascript看看是否在登录时创建的用户会话是否存在?



我尝试过:



i尝试了类似下面的内容,但似乎没有工作

 if('<%= Session(UserSessionID)%>'== null) 
{
window.location(login)
}
{

解决方案

你在这两个世界混合,这永远不会结束。没有任何进一步的复杂性,我建议您将这些元素带到客户端,并考虑使用JavaScript API,如 sessionStorage 来维护此会话。现在,由于会话值处于脱机状态,因此不需要回发并更新值,因为可以查询浏览器以检查会话是否存在或是否过期。



Window.sessionStorage - Web API | MDN [ ^ ]

Hi Guys

I am currently busy on an application in MVC and have a search method on screen that calls the ActionResult in the backend code, all works well the issue i picked up is if the user leaves the page and the session times out and the user tries to search because the search doesnt do an actual postback it think it is not picking up the session has expired so the Javascript runs and shows the search animation to say it is searching but nothing is really happening on the page and the actionresult is never called cause the session has expired.

How could i get the Javascript to see if the user session created on login exists or not?

What I have tried:

i have tried something like the below but didnt seem to work

if ('<%=Session("UserSessionID")%>' == null)
{
window.location("login")
}
        {

解决方案

You are mixing the two worlds here, and that is never going to end up well. Without any further complications, I would recommend that you bring these elements to the client-side and consider using JavaScript APIs such as sessionStorage to maintain this session. Now, since the session values are offline, it doesn't require a postback and updating the values because browser can be queried to check if the session exists or did it expire.

Window.sessionStorage - Web APIs | MDN[^]


这篇关于如何检查MVC应用程序中角度js中是否存在会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆