响应重定向使session_end成为全局的asax

时间:2012-12-26 11:36:11

标签: c# asp.net asp.net-3.5 response.redirect

由于会话变为空,我感到很沮丧。
当我点击“保存”按钮时,我将数据从后面的代码保存到数据库中,如果成功,我将用户重定向到主项目页面,使用语法:

Response.Redirect("~/Admin/Projects.aspx?i=esc&prjName=abc",'false');

但它使我的会话为空。
它转到Globex.asax页面ang执行Session_End并使所有会话为空。

我甚至尝试了Server.Transfer但是这样浏览器网址仍然保持不变,客户端不想要这个。甚至在某些地方我读到Server.Execute也用于重定向,但它显示有些错误结果

我可以使用Response.Redirect没有此会话空问题吗?

(在此页面中,我创建了一个文本文件来存储一些长描述,如果成功,那么我将重定向到另一个页面。)

更新::

这是按钮点击的代码

protected void lnkbtnAddDescription_Click(object sender, EventArgs e)
        {
            try
            {
                if ((!hidProjId.Value.ToString().Equals("") || !hidEditProjId.Value.ToString().Equals("")) && !txtDescription.Value.ToString().Equals(""))
                {
                    //ProjectDescription
                    int projId = 0;
                    if (!hidIsEdit.Value.ToString().Equals(""))
                    {
                        projId = Convert.ToInt32(hidEditProjId.Value.ToString());
                    }
                    else
                    {
                        projId = Convert.ToInt32(hidProjId.Value.ToString());
                    }

                    ProjectM proj = new ProjectM();
                    proj.LoadByKey(projId);

                    string prj = proj.ProjectName.ToString().Replace(" ", "-");

                    string strDirectoryPath = Server.MapPath("~/ProjectDescription/") + proj.ProjectId + "-" + prj;
                    if (!Directory.Exists(strDirectoryPath))
                    {
                        Directory.CreateDirectory(strDirectoryPath);

                        string filePath = strDirectoryPath + "/" + proj.ProjectId + "-" + prj + ".txt";
                        string strDescription = txtDescription.Value.ToString().Replace("<br />", "<p>");
                        createTextFile(filePath, strDescription);

                        string dbDirectoryPath = "~/ProjectDescription/" + proj.ProjectId + "-" + prj + "/" + proj.ProjectId + "-" + prj + ".txt";
                        proj.Description = dbDirectoryPath.ToString();
                        proj.IsNew = false;
                        proj.Save();

                        if (!hidIsEdit.Value.ToString().Equals(""))
                        {
                            //Server.Execute("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString() + "",false);
                            Session["dsProj"] = null;
                            Session["editProjId"] = null;
                            Session["fname"] = hidFname.Value.ToString();
                            Session["UserId"] = hidUserId.Value.ToString();
                            Session["role"] = hidRole.Value.ToString();
                            Response.Redirect("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString());
                        }
                        else
                        {
                            //Server.Execute("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString() + "",false);
                            Session["dsProj"] = null;
                            Session["editProjId"] = null;
                            Session["fname"] = hidFname.Value.ToString();
                            Session["UserId"] = hidUserId.Value.ToString();
                            Session["role"] = hidRole.Value.ToString();
                            Response.Redirect("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString());
                        }

                    }
                    else
                    {
                        Directory.Delete(strDirectoryPath, true);
                        Directory.CreateDirectory(strDirectoryPath);

                        string fileName = proj.ProjectName.ToString().Replace(" ", "-");

                        string filePath = strDirectoryPath + "/" + proj.ProjectId + "-" + fileName + ".txt";
                        string strDescription = txtDescription.Value.ToString().Replace("<br>", "<p>");
                        createTextFile(filePath, strDescription);

                        string dbDirectoryPath = "~/ProjectDescription/" + proj.ProjectId + "-" + proj.ProjectName.ToString() + "/" + proj.ProjectId + "-" + proj.ProjectName.ToString() + ".txt";
                        proj.Description = dbDirectoryPath.ToString();
                        proj.IsNew = false;
                        proj.Save();

                        if (!hidIsEdit.Value.ToString().Equals(""))
                        {
                            //Server.Execute("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString() + "", false);
                            Session["dsProj"] = null;
                            Session["editProjId"] = null;
                            Session["fname"] = hidFname.Value.ToString();
                            Session["UserId"] = hidUserId.Value.ToString();
                            Session["role"] = hidRole.Value.ToString();
                            Response.Redirect("~/SuperAdmin/Projects.aspx?i=esc&prjName=" + proj.ProjectName.ToString());
                        }
                        else
                        {
                            //Server.Execute("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString() + "", false);
                            Session["dsProj"] = null;
                            Session["editProjId"] = null;
                            Session["fname"] = hidFname.Value.ToString();
                            Session["UserId"] = hidUserId.Value.ToString();
                            Session["role"] = hidRole.Value.ToString();
                            Response.Redirect("~/SuperAdmin/Projects.aspx?i=sc&prjName=" + proj.ProjectName.ToString());
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void createTextFile(string filePath, string strDescription)
        {
            try
            {   
                StreamWriter w = File.CreateText(filePath);
                w.WriteLine(strDescription);
                w.Flush();
                w.Close();
            }
            catch (Exception ex)
            {

            }
        }

4 个答案:

答案 0 :(得分:0)

在重定向到下一页之前, 在按钮单击事件中也分配现有会话值。

这可以解决问题。

答案 1 :(得分:0)

希望这会对你有所帮助, ASP.NET Session becomes null after postback on local

这认为您的问题与某种权限拒绝有关,即nulling您的会话。

答案 2 :(得分:0)

创建或保存文本文件时一定存在问题。 这可能正在调用Session_End,因此您将所有值都设为null。

答案 3 :(得分:-1)