本文介绍了c# - asp.net ::会话值在Response.Redirect(Request.Url.AbsoluteUri)后变为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嘿伙计们,我正在制作一个视频处理程序,我想在我的网页上播放来自数据库的视频。 但是这个网站是基于用户的所以我有会话保持我的信息在页面之间运行。 问题发生在我选择了视频文件后我按上传。在Response.Redirect之后(Request.Url.AbsoluteUri) ); 所以我得到null异常.. 我试图使用Response.Redirect(〜/ Default.aspx?Email =+ PID); 上传后页面加载好但是我没有播放视频,所以我想是否需要使用absoluteURI来播放视频... 有什么想法吗?这是代码,东西发生在BindGrid();和bttnUpload。 谢谢:) 命名空间 ShowingImages { public partial class 默认值:System.Web.UI.Page { public string query,constr,query1,query2,query3; public SqlConnection con,conn; public void connection() { constr = ConfigurationManager。 ConnectionStrings [ Myconnection]。ToString(); con = new SqlConnection(constr); con.Open(); } 受保护 void Page_Load( object sender,EventArgs e) { Label1.Visible = 假; PID =( int )(会话[ ID]); if (!IsPostBack) { HttpContext context = HttpContext.Current ; UserProfile objUserProfile = Session [ UserData _ + PID] as UserProfile; lblemail.Text = objUserProfile.First_Name; lblname.Text = objUserProfile.Email_Account; if (PID == null ) { Response.Redirect( Login.aspx); } imagebindGrid(); PostSelection(); } } private void InitializeComponent() { throw new NotImplementedException(); } 受保护 void 上传( object sender,EventArgs e) // buttonγιατοuploadτης φωτο。 ΚαλειταιηκλασηImageupload() { Imageupload(); } / * Κλασσηγιαuploadεικονας。 Ελεγχοςκαιπερασμαεικονας* / private void Imageupload() { if (FileUpload1.HasFile) { PID =( int )(会话[ id]); if (PID!= null ) { int imagefilelenth = FileUpload1.PostedFile.ContentLength; byte [] imgarray = new byte [imagefilelenth]; HttpPostedFile image = FileUpload1.PostedFile; image.InputStream.Read(imgarray, 0 ,imagefilelenth); connection(); query = 插入ImageToDB(user_id,ImageName,Image)值(@ user_id,@ Name,@图像); SqlCommand com = new SqlCommand(query,con); com.Parameters.AddWithValue( @ Name,SqlDbType.VarChar).Value = TextBox1.Text; com.Parameters.AddWithValue( @ Image,SqlDbType.Image).Value = imgarray; com.Parameters.AddWithValue( @ user_id,PID); com.ExecuteNonQuery(); Label1.Visible = true ; Label1.Text = 图像已成功上传; imagebindGrid(); BindGrid(); } } } / * Gridviewγιαεικονες* / public void imagebindGrid() { connection(); // SQL注入预防! query = SELECT id,ImageName,ImageToDB中的图像,其中user_id =(@ PerID); SqlCommand com = new SqlCommand(query,con); com.Parameters.Add( new SqlParameter( PerID,PID)); // com.ExecuteReader(); SqlDataReader dr = com.ExecuteReader (); dr.Read(); Image1.ImageUrl = Handler1.ashx?id_Image = + PID; } / * 退出按钮* / 受保护 void Button1_Click( object sender,EventArgs e) { System.Web.Security.FormsAuthentication.SignOut(); Session.Clear(); Session.RemoveAll(); Session.Abandon(); Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); HttpContext.Current.Response.AddHeader( Cache-Control, no-cache,no-store,must-revalidate); HttpContext.Current.Response.AddHeader( Pragma, no-cache); HttpContext.Current.Response.AddHeader( Expires, 0); FormsAuthentication.SignOut(); HttpContext.Current.User = new GenericPrincipal( new GenericIdentity( string .Empty), null ); Response.Redirect( 〜/ Logout.aspx); } / * ΚλασσηγιατοPost* / private void Txt() { 尝试 { PID =( int )(会话[ id]); if (PID!= null ) { connection( ); query1 = 插入Posttext(user_id,Posttext)值(@ user_id,@ Your_Post); SqlCommand com2 = new SqlCommand(query1,con); com2.Parameters.AddWithValue( @ Your_Post,SqlDbType.VarChar).Value = PostBox.Text; com2.Parameters.AddWithValue( @ user_id,PID); com2.ExecuteNonQuery(); lbluser.Text = PostBox.Text; PostSelection(); } } catch (例外情况) { // con.Close(); } } / * Κανει选择τακειμενακαιταανεβαζειαποτηνβασηστο网格* / private void BindGrid() { string strConnString = ConfigurationManager.ConnectionStrings [ Myconnection ]的ConnectionString。 使用(SqlConnection con = new SqlConnection(strConnString)) { 使用(SqlCommand cmd = new SqlCommand()) { cmd .CommandText = select * tblFiles; cmd.Connection = con; con.Open(); DataList1.DataSource = cmd.ExecuteReader(); DataList1.DataBind(); con.Close(); } } } 受保护 void btnUpload_Click( object sender,EventArgs e) { using ( BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream)) { byte [] bytes = br.ReadBytes(( int )FileUpload1.PostedFile.InputStream.Length); string strConnString = ConfigurationManager.ConnectionStrings [ MyConnection的]的ConnectionString。 使用(SqlConnection con = new SqlConnection(strConnString)) { 使用(SqlCommand cmd = new SqlCommand()) { cmd .CommandText = 插入到tblFiles(Name,ContentType,Data)值(@ Name,@ ContentType,@ Data) ; cmd.Parameters.AddWithValue( @ Name,Path.GetFileName(FileUpload1.PostedFile) 。文件名)); cmd.Parameters.AddWithValue( @ ContentType, video / mp4); cmd.Parameters.AddWithValue( @ Data,bytes); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } Response.Redirect(Request.Url.AbsoluteUri); // Response.Redirect(〜/ Default.aspx?Email =+ PID); } public void PostSelection () { connection(); // SQL注入预防!!!! query2 = 从Posttext中选择Posttext,其中user_id =(@ PerID); SqlCommand com1 = new SqlCommand(query2,con); com1.Parameters.Add( new SqlParameter( PerID,PID)); SqlDataReader Read = com1.ExecuteReader(); grdemployee7.DataSource = Read; grdemployee7.DataBind(); Read.Close(); } / * ΚαλειταιηTxt()γιαναγινειτοPost* / 受保护 void Button2_Click( object sender,EventArgs e) { Txt(); } public string USER_PID {获得; set ; } public DateTime _myid { get ; set ; } / * --------------- -----Κουμπιγια搜索PROFILE ----------------------------------- * / public void Button3_Click1( object sender,EventArgs e) { Response.Redirect( 〜/ WebForm7.aspx ); } public int PID { get ; set ; } protected void PostBox_TextChanged( object sender,EventArgs e) { } public string sqlquer { get ; set ; } public object SqlQueryIn {获得; set ; } 受保护 void Button4_Click(对象发件人,EventArgs e) { } } } 解决方案 查看MSDN文档: http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx [ ^ ] 将endResponse指定为false参数: Response.Redirect(url,false); hey guys, im making a video handler and i want to play videos on my webpage from DB.but this site is user based so i have sessions to keep my info running between pages.The problem occurs when after i've chosen the video file i press upload.After the Response.Redirect(Request.Url.AbsoluteUri); So i get null exception..I tried to use Response.Redirect("~/Default.aspx?Email=" + PID);the page loaded ok after the upload but i didn't have the video playing, so im thinking if absoluteURI is needed for the video to work...Any ideas? here's the code , stuff happens at BindGrid(); and bttnUpload.THANKS :)namespace DisplayingImages{ public partial class Default : System.Web.UI.Page { public string query, constr, query1, query2, query3; public SqlConnection con, conn; public void connection() { constr = ConfigurationManager.ConnectionStrings["Myconnection"].ToString(); con = new SqlConnection(constr); con.Open(); } protected void Page_Load(object sender, EventArgs e) { Label1.Visible = false; PID = (int)(Session["id"]); if (!IsPostBack) { HttpContext context = HttpContext.Current; UserProfile objUserProfile = Session["UserData_" + PID] as UserProfile; lblemail.Text = objUserProfile.First_Name; lblname.Text = objUserProfile.Email_Account; if (PID == null) { Response.Redirect("Login.aspx"); } imagebindGrid(); PostSelection(); } } private void InitializeComponent() { throw new NotImplementedException(); } protected void upload(object sender, EventArgs e) // button για το upload της φωτο. Καλειται η κλαση Imageupload() { Imageupload(); } /* Κλασση για upload εικονας . Ελεγχος και περασμα εικονας */ private void Imageupload() { if (FileUpload1.HasFile) { PID = (int)(Session["id"]); if (PID != null) { int imagefilelenth = FileUpload1.PostedFile.ContentLength; byte[] imgarray = new byte[imagefilelenth]; HttpPostedFile image = FileUpload1.PostedFile; image.InputStream.Read(imgarray, 0, imagefilelenth); connection(); query = "Insert into ImageToDB (user_id,ImageName,Image) values (@user_id,@Name,@Image)"; SqlCommand com = new SqlCommand(query, con); com.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = TextBox1.Text; com.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imgarray; com.Parameters.AddWithValue("@user_id", PID); com.ExecuteNonQuery(); Label1.Visible = true; Label1.Text = "Image Is Uploaded successfully"; imagebindGrid(); BindGrid(); } } } /* Gridview για εικονες */ public void imagebindGrid() { connection(); //SQL Injection Prevention! query = "SELECT id,ImageName,Image from ImageToDB where user_id= (@PerID)"; SqlCommand com = new SqlCommand(query, con); com.Parameters.Add(new SqlParameter("PerID", PID)); // com.ExecuteReader(); SqlDataReader dr = com.ExecuteReader(); dr.Read(); Image1.ImageUrl = "Handler1.ashx?id_Image=" + PID; } /*Logout Button */ protected void Button1_Click(object sender, EventArgs e) { System.Web.Security.FormsAuthentication.SignOut(); Session.Clear(); Session.RemoveAll(); Session.Abandon(); Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.Cache.SetNoStore(); HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate"); HttpContext.Current.Response.AddHeader("Pragma", "no-cache"); HttpContext.Current.Response.AddHeader("Expires", "0"); FormsAuthentication.SignOut(); HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null); Response.Redirect("~/Logout.aspx"); } /* Κλασση για το Post */ private void Txt() { try { PID = (int)(Session["id"]); if (PID != null) { connection(); query1 = "Insert into Posttext (user_id,Posttext) values (@user_id,@Your_Post)"; SqlCommand com2 = new SqlCommand(query1, con); com2.Parameters.AddWithValue("@Your_Post", SqlDbType.VarChar).Value = PostBox.Text; com2.Parameters.AddWithValue("@user_id", PID); com2.ExecuteNonQuery(); lbluser.Text = PostBox.Text; PostSelection(); } } catch (Exception ex) { //con.Close(); } } /* Κανει select τα κειμενα και τα ανεβαζει απο την βαση στο grid */ private void BindGrid() { string strConnString = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "select * tblFiles"; cmd.Connection = con; con.Open(); DataList1.DataSource = cmd.ExecuteReader(); DataList1.DataBind(); con.Close(); } } } protected void btnUpload_Click(object sender, EventArgs e) { using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream)) { byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length); string strConnString = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString; using (SqlConnection con = new SqlConnection(strConnString)) { using (SqlCommand cmd = new SqlCommand()) { cmd.CommandText = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)"; cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName)); cmd.Parameters.AddWithValue("@ContentType", "video/mp4"); cmd.Parameters.AddWithValue("@Data", bytes); cmd.Connection = con; con.Open(); cmd.ExecuteNonQuery(); con.Close(); } } } Response.Redirect(Request.Url.AbsoluteUri); // Response.Redirect("~/Default.aspx?Email=" + PID); } public void PostSelection() { connection(); //SQL Injection Prevention!!!! query2 = "Select Posttext from Posttext where user_id= (@PerID)"; SqlCommand com1 = new SqlCommand(query2, con); com1.Parameters.Add(new SqlParameter("PerID", PID)); SqlDataReader Read = com1.ExecuteReader(); grdemployee7.DataSource = Read; grdemployee7.DataBind(); Read.Close(); } /* Καλειται η Txt() για να γινει το Post */ protected void Button2_Click(object sender, EventArgs e) { Txt(); } public string USER_PID { get; set; } public DateTime _myid { get; set; } /* --------------------Κουμπι για search PROFILE -----------------------------------*/ public void Button3_Click1(object sender, EventArgs e) { Response.Redirect("~/WebForm7.aspx"); } public int PID { get; set; } protected void PostBox_TextChanged(object sender, EventArgs e) { } public string sqlquer { get; set; } public object SqlQueryIn { get; set; } protected void Button4_Click(object sender, EventArgs e) { } }} 解决方案 Look at MSDN documentation:http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx[^]Specify the endResponse as false parameter:Response.Redirect(url, false); 这篇关于c# - asp.net ::会话值在Response.Redirect(Request.Url.AbsoluteUri)后变为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 08:14