如何清除静态变量值和传入新的

本文关键字:变量值 静态 何清除 清除 | 更新日期: 2023-09-27 18:11:00

我从MVC项目中调用aspx网站并传递两个参数

在全球。asax在Session_start我将这个参数分配给两个静态变量

protected void Session_Start(object sender, EventArgs e)
        {
            global.c = Request.QueryString["C"];
            global.u = Request.QueryString["U"];
        }

这个aspx网站没有登出。第一次值分配正确,这是从MVC传递但是对于下一个请求,它显示旧的值。

如:https://www.xxxxxx.com/aa.aspx?C=2& U = 2

这次会显示2和2

下次如果我调用https://www.xxxxxx.com/aa.aspx?C=32&U=14

将保留2和2。因为没有登出,所以不能使用

System.Web.Security.FormsAuthentication.SignOut();

由于会话将激活20分钟,默认情况下该值将在那里。

如何在aspx中全局存储这些参数并在所有页面上使用

多个用户可以同时使用不同的参数访问这个aspx页面

如何清除静态变量值和传入新的

请先检查基本功能。

STEP1:把调试器放在session_start事件上。当您第一次使用

时,断点将激活。

https://www.xxxxxx.com/aa.aspx?C=2& U = 2

STEP2:现在再次点击更改查询字符串的页面,您将注意到断点将不会在time time命中。

原因:会话启动只在会话创建时触发一次,并且在整个会话中为相同的值工作

解决方案:在客户端保存一些临时值,因为您已经在页面加载时使用查询字符串完成了。

PAGE1: Assign cookie value

 protected void Page_Load(object sender, EventArgs e)
    {
    if (Request.Browser.Cookies)
        {
            //supports the cookies
         Response.Cookies["MyCookies"].Value = Request.QueryString["id"];
        }
    else
    {
        //not supports the cookies
        //redirect user on specific page
        //for this or show messages
    }
    }

PAGE2: Read Cookie value

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Cookies["MyCookies"] != null)
    {
        string userSettings = Request.Cookies["MyCookies"].Value;
    }
}

您可以遵循触发会话