如何在ASP.Net MVC中的RedirectToAction中传递tempdata

时间:2021-08-29 16:55:03

I need to pass one logout successful message in one of the views but I am not able to do so. Here is what I have.

我需要在其中一个视图中传递一个注销成功消息,但我无法这样做。这就是我所拥有的。

Not Working Solution:

不工作解决方案:

 //LogController:
  public ActionResult Logoff()
  {
      DoLogOff();
      TempData["Message"] = "Success";
      return RedirectToAction("Index", "Home");
  }

  // HomeController
  public ActionResult Index()
  {
      return View();
  }

Index CSHTML File:

索引CSHTML文件:

@Html.Partial("../Home/DisplayPreview")

DisplayPreview CSHTML File:

DisplayPreview CSHTML文件:

   @TempData["Message"] 

Working Solution

public ActionResult Logoff()
{
     DoLogOff();
     return RedirectToAction("Index", "Home", new { message = "Logout Successful!" });
}

public ActionResult Index(string message)
{
    if (!string.IsNullOrEmpty(message))
        TempData["Message"] = message;
    return View();
}

Index CSHTML File:

索引CSHTML文件:

   @TempData["Message"] 

But I want something like my first solution.

但我想要像我的第一个解决方案。

3 个解决方案

#1


0  

See if this works:

看看这是否有效:

public ActionResult Logoff()
{
    DoLogOff();
    ControllerContext.Controller.TempData["Message"] = "Success";
    return RedirectToAction("Index", "Home");
}

#2


0  

In the controller;

在控制器中;

public ActionResult Index()
{
    ViewBag.Message = TempData["Message"];
    return View();
}
public ActionResult Logoff()
{
    DoLogOff();
    TempData["Message"] = "Success";
    return RedirectToAction("Index", "Home");
}

Then you can use it in view like;

然后你可以在视图中使用它;

@ViewBag.Message

#3


0  

Since you don't show what DoLogOff() does, my guess is that you are abandoning the session, which means any data stored in session (like TempData) is lost. A new session does not get generated until the next page refresh, so it doesn't work.

由于您没有显示DoLogOff()的功能,我猜测您正在放弃会话,这意味着会话中存储的任何数据(如TempData)都将丢失。在下一页刷新之前不会生成新会话,因此它不起作用。

What you might try is simply passing a flag to your Index view that will show the logged off message if it's present. I would NOT use the string message, like you show in your "working" example, because this can be coopted by attackers to redirect people to malicious sites.

你可能尝试的只是将一个标志传递给你的索引视图,如果它存在,它将显示已注销的消息。我不会像你在“工作”示例中所显示的那样使用字符串消息,因为这可以被攻击者用来将人们重定向到恶意网站。

#1


0  

See if this works:

看看这是否有效:

public ActionResult Logoff()
{
    DoLogOff();
    ControllerContext.Controller.TempData["Message"] = "Success";
    return RedirectToAction("Index", "Home");
}

#2


0  

In the controller;

在控制器中;

public ActionResult Index()
{
    ViewBag.Message = TempData["Message"];
    return View();
}
public ActionResult Logoff()
{
    DoLogOff();
    TempData["Message"] = "Success";
    return RedirectToAction("Index", "Home");
}

Then you can use it in view like;

然后你可以在视图中使用它;

@ViewBag.Message

#3


0  

Since you don't show what DoLogOff() does, my guess is that you are abandoning the session, which means any data stored in session (like TempData) is lost. A new session does not get generated until the next page refresh, so it doesn't work.

由于您没有显示DoLogOff()的功能,我猜测您正在放弃会话,这意味着会话中存储的任何数据(如TempData)都将丢失。在下一页刷新之前不会生成新会话,因此它不起作用。

What you might try is simply passing a flag to your Index view that will show the logged off message if it's present. I would NOT use the string message, like you show in your "working" example, because this can be coopted by attackers to redirect people to malicious sites.

你可能尝试的只是将一个标志传递给你的索引视图,如果它存在,它将显示已注销的消息。我不会像你在“工作”示例中所显示的那样使用字符串消息,因为这可以被攻击者用来将人们重定向到恶意网站。