tempdata不会持续到第二个动作
我正在尝试通过几个动作传递tempdata
,但我无法使其持续超过一个通过。我已经在Stackoverflow上阅读了很多有关此问题的问题,但是我无法获得他们的工作解决方案。我知道tempdata
仅用于一个重定向,但建议.keep()或.peek()应允许其持续在另一个重定向上。不幸的是,这对我没有用。我还尝试了重新分配tempdata
,并从第二个重定向中进行了tempdata
的直接硬编码,但它仍然不会通过。我显然缺少一些东西。我的代码:
//First redirect
public ActionResult Index(int? userId, int? reportingYear)
{
if (Session["State"] == null)
{
TempData["Timeout"] = "Your session has timed out. Please login to continue.";
return RedirectToAction("LogOff", "Account");
}
}
//Second redirect
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult LogOff()
{
//Delete the application cookie and clear session variables
var cookies = Request.Cookies;
List<string> tempCookies = new List<string>();
foreach (string cookie in cookies)
{
if (cookie.ToString() != "quailCoord")
{
tempCookies.Add(cookie);
};
}
foreach (string cookie in tempCookies)
{
HttpCookie deleteCookie = Request.Cookies[cookie];
deleteCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(deleteCookie);
}
Session.Abandon();
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
//When checking that the key exists, it does and enters the if statement to keep the data
if (TempData.ContainsKey("Timeout")
{
TempData.Keep("Timeout");
}
return RedirectToAction("Login");
}
//Third action
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
//It does not find any TempData keys
if (TempData.ContainsKey("Timeout"))
{
ViewBag.Timeout = TempData["Timeout"] as string;
}
return View();
}
我还尝试过这些代替tempdata.keep(“ timeout”)
方法:
tempdata.peek(“ timeout”)
tempdata [''超时” = tempdata [“ timeout”]
。
tempdata [“ timeout”] =“您的会话已经定时登录 >登录()
操作。 tempdata
在输入该操作时始终是空的。调试时,我跨越返回redirecttoAction(“ login”)
行的分钟,tempdata中的计数转为0。我缺少什么?删除cookie是一个问题吗?
I'm trying to pass TempData
through a couple actions, but I can't get it to persist beyond one pass. I've read a ton of questions on StackOverflow about this, but I just can't get their solutions to work. I know TempData
only persists for one redirect, but it is suggested that .Keep() or .Peek() should allow it to persist on another redirect. That unfortunately has not worked for me. I have also tried reassigning TempData
and straight hard-coding of TempData
from the second redirect and it still will not pass through. I'm obviously missing something. My code:
//First redirect
public ActionResult Index(int? userId, int? reportingYear)
{
if (Session["State"] == null)
{
TempData["Timeout"] = "Your session has timed out. Please login to continue.";
return RedirectToAction("LogOff", "Account");
}
}
//Second redirect
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult LogOff()
{
//Delete the application cookie and clear session variables
var cookies = Request.Cookies;
List<string> tempCookies = new List<string>();
foreach (string cookie in cookies)
{
if (cookie.ToString() != "quailCoord")
{
tempCookies.Add(cookie);
};
}
foreach (string cookie in tempCookies)
{
HttpCookie deleteCookie = Request.Cookies[cookie];
deleteCookie.Expires = DateTime.Now.AddDays(-1);
Response.Cookies.Add(deleteCookie);
}
Session.Abandon();
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
//When checking that the key exists, it does and enters the if statement to keep the data
if (TempData.ContainsKey("Timeout")
{
TempData.Keep("Timeout");
}
return RedirectToAction("Login");
}
//Third action
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
//It does not find any TempData keys
if (TempData.ContainsKey("Timeout"))
{
ViewBag.Timeout = TempData["Timeout"] as string;
}
return View();
}
I have also attempted these in place of the TempData.Keep("Timeout")
method:
TempData.Peek("Timeout")
TempData["Timeout"] = TempData["Timeout"]
TempData["Timeout"] = "Your session has timed out. Please login to continue."
None of these methods pass to the Login()
action. TempData
is always empty upon entering that action. When debugging, the minute I step over the return RedirectToAction("Login")
line, the count in TempData turns to 0. What am I missing? Is deleting the cookies a problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
tempdata
将将数据存储在session
可能存储 sessionId 在cookie中,如果您删除该服务器将创建另一个 sessionIdId (对象)为您而不是原始一个。因此,如果您想将
tempdata
保留到多个操作中,我们可能需要保留 sessionId cookie的值。从您的代码中,我们可以尝试添加
检查cookie密钥的判断是否不删除cookie。
Because
TempData
will store data inSession
object which might store SessionId in cookie, if you delete that server will create another SessionId (object) for you instead of the original one.so that if you want to keep
TempData
to multiple actions we might need to keep SessionId value from the cookie.From your code, we can try to add
a judgement to check cookie key whether if yes don't delete the cookie.