Session变量不保存在初始页面加载 [英] Session variables not saving on initial page load

查看:122
本文介绍了Session变量不保存在初始页面加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序这台基于内部静态IP地址的一些会话变量。

I have an MVC application which sets some session variables based on internal static IP addresses.

我已经建立了一个ApplicationController中覆盖MVC中OnActionExecuted子的,我需要在我的应用程序中使用的数据。

I have built an ApplicationController to override the OnActionExecuted sub in MVC for data that I need to use throughout my application.

但是,下面的code,这仅仅是一个我的code的片段,但编辑对我的职务,只是部分有效。在初始页面加载,会话变量不会被保存,但页面刷新后他们。我的问题是,这些需要被保存在初始页面加载。

However, the code below, which is just a snippet of my code but is edited for my post, only partially works. On initial page load, the session variables aren't saved, but after a page refresh they are. The issue I have is that these need to be saved on the initial page load.

If Session("Item1") = Nothing Then
                If IpAddressShort <> "" Then
                    Dim locInfo = cmsRepository.GetInfoBasedOnLocation(IpAddressShort).SingleOrDefault()

                    If locInfo IsNot Nothing Then
                        Session("Item1") = locInfo.Item1
                        Session("Item2") = locInfo.Item2
                        Session("Item3") = locInfo.Item3
                        Session("Item4") = locInfo.Item4

                        If locInfo.Item2= "1" Then
                            Session("Visibility") = 3
                            Session("TypeShort") = "XXXX"
                        ElseIf locInfo.Item2= "2" Then
                            Session("Visibility") = 4
                            Session("TypeShort") = "YYYY"
                        ElseIf locInfo.Item2= "9" Then
                            Session("Visibility") = 2
                            Session("TypeShort") = "ZZZZZ"
                        End If

                    End If
                End If
            End If

从理论上说,如果我是正确的,如果没有会议(项目1)设置/如果会议(项目1)为空,则该片段的其余部分应该运行,并设置这些变量。

Theoretically, if I'm correct, if there is no Session("Item1") set/if Session("Item1") is empty, then the rest of the snippet should run and set those variables.

如何走到这一步的第一次不设置这些变量页面加载速度?

How come this isn't setting those variables on the first time the page load?

感谢所有帮助提前

推荐答案

我假设你确定会话数据并不present因为你不能从你的操作方法中访问它。

I assume that you are determining that the session data is not present because you cannot access it from within your action method.

如果是这种情况,那么尝试重写 OnActionExecuting ,而不是因为这是操作方法之前调用。 OnActionExecuted 的操作方法后调用。

If this is the case then try overriding OnActionExecuting instead as this is called before the action method. OnActionExecuted is called after the action method.

这篇关于Session变量不保存在初始页面加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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