问题描述
我尝试添加会话,然后在重定向后访问另一个页面,这始终显示为空
I tried to add session and then i accessed on another page after redirect this always show null
Session.Add("pUser_Name", ds_1.Tables[0].Rows[0]["User_Name"].ToString());
HttpContext.Current.ApplicationInstance.CompleteRequest();
Response.Redirect("DashBoard.aspx", false);
在这里我正在访问的另一个页面上
here on another page i am accessing like this
protected void Page_Load(object sender, EventArgs e)
{
if (Session["pUser_Name"] == null) ////here is error NULL
{
Response.Redirect("Admin.aspx");
}
}
我试过了
protected override void OnInit(EventArgs e)
{
if (Session["pUser_Name"] == null)
{
Response.Redirect("Admin.aspx");
}
base.OnInit(e);
}
还有
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DashBoard.aspx.cs" Inherits="SC.DashBoard" EnableTheming="true"
Theme="Theme1" EnableSessionState="True" %>
和管理页面,其中为会话分配值
and Admin page where assigning value to session
<%@ Page Language="C#" AutoEventWireup="true" EnableSessionState="True" CodeBehind="Admin.aspx.cs" Inherits="SC.Admin" %>
和 web.config 文件
and web.config file
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<sessionState timeout="12000"/>
</system.web>
但仍然面临相同的会话为空
but still facing same session is null
推荐答案
基于此 answer 基本上指的是这个 msdn 文章关于 SessionID
,您可能需要在 Session_Start 上初始化您的会话对象:
Based on this answer which basically refers to this msdn article about SessionID
, you might need to initialise your session object on Session_Start:
protected void Session_Start(Object sender, EventArgs e)
{
Session["init"] = 0;
}
当会话状态存储在 cookie 中时,这是必需的,您当前根据 web.config 使用该 cookie.原因是 ASP.NET 在第一次使用之前不会保存会话,因此当您 Response.Redirect时发生的每个 new 页面请求都会生成一个新的 SessionID代码>到另一个页面.
This is required when session state is stored in a cookie, which you currently use based on your web.config. The reason is that ASP.NET does not save session up until its first usage and as a result a new SessionID is generated with each new page request which is happening when you Response.Redirect
to another page.
这篇关于重定向到另一个页面时会话始终为空?C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!