Response.Redirect("x.aspx);跳转后session为null的解决方法
2010-05-14 23:54
633 查看
通常我们做登陆的时候都是登录成功后为管理员保存一些信息,一般都会写类似下面的代码
if(登录成功)
{
Session["xx"] = "user";
Response.Redirect("Framework.aspx");
}
当我们登录后再从其它页面访问Session["xx"]的时候发现报错说为null.郁闷具体是为什么我也不清楚.
网上百度一下有人做了如下解释,不知道是否合理
原因:当asp.net 执行 response.redirect 时会强制终止当前response ,不发送当前页面的cookie 给浏览器,而是发送一个指令告诉浏览器重新发送一个新的http请求到新的url,结果导致当前的session 丢失。
解决这个问题的方法是在当前页面接受get请求时就设置一下session ,这样浏览器就会记录下当前的session id,当浏览器再发送post 请求到当前页面时,server就不需要再发送一次session id 的cookie 给浏览器了。
所以为了解决这个问题我改成这样写了。
if(登录成功)
{
TempData["xx"] = "user";
Response.Redirect("a.aspx");
}
然后在a.aspx里写
Session["xx"]=TempData["xx"].ToString();
这个问题就解决了……
注意我是用MVC开发的,webform好像没有TempData["xx"]这个东东的……
if(登录成功)
{
Session["xx"] = "user";
Response.Redirect("Framework.aspx");
}
当我们登录后再从其它页面访问Session["xx"]的时候发现报错说为null.郁闷具体是为什么我也不清楚.
网上百度一下有人做了如下解释,不知道是否合理
原因:当asp.net 执行 response.redirect 时会强制终止当前response ,不发送当前页面的cookie 给浏览器,而是发送一个指令告诉浏览器重新发送一个新的http请求到新的url,结果导致当前的session 丢失。
解决这个问题的方法是在当前页面接受get请求时就设置一下session ,这样浏览器就会记录下当前的session id,当浏览器再发送post 请求到当前页面时,server就不需要再发送一次session id 的cookie 给浏览器了。
所以为了解决这个问题我改成这样写了。
if(登录成功)
{
TempData["xx"] = "user";
Response.Redirect("a.aspx");
}
然后在a.aspx里写
Session["xx"]=TempData["xx"].ToString();
这个问题就解决了……
注意我是用MVC开发的,webform好像没有TempData["xx"]这个东东的……
相关文章推荐
- Response.Redirect("x.aspx);跳转后session为null的解决方法
- aspx在页面跳转(Response.Redirect)时丢失session问题及解决办法
- ubuntu12.04.4下出现"(sublime:12386): Gtk-WARNING **: Failed to load type module: (null)"问题及解决方法
- 网页跳转:Server的Transfer和Response的Redirect方法(总结)
- An internal error occurred during: "Launching xx". java.lang.NullPointerException的一个解决方法
- 元素类型为 "session-factory" 的内容必须匹配 "(property*,mapping*,(class-cach....解决方法
- Fiddler获取ResponseBody乱码问题解决方法----oSession.GetResponseBodyAsString();
- 实现springmvc 的controller级别的跳转的方法 return "redirect:/news/to";
- Ajax Throws Sys.WebForms.PageRequestManagerErrorException with Response.Redirect的解决方法
- 解决调用context.Session["NAME"]时总出现Object reference not set to an instance of an object.异常的方法
- ((Login)(this)).AppRelativeVirtualPath = "~/login.aspx 错误的解决方法
- [转]Null value was assigned to a property of primitive type setter of"原因及解决方法
- "Cannot forward after response has been committed "错误的正确解决方法
- frame框架-用户session失效仅在iframe中跳转登录页面的解决方法
- Spring MVC使用@ResponseBody无法跳转到对应的jsp页面以及ie8下报错的解决方法
- Response.Redirect导致session丢失的解决办法
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- Null value was assigned to a property of primitive type setter of"原因及解决方法
- Response.Redirect("");Server.Transfer("")跳转页面的区别
- Document root element *, must match DOCTYPE root "null"分析及解决方法