C# 在ASP.NETMVC中如何在RedirectToAction中传递tempdata

C# 在ASP.NETMVC中如何在RedirectToAction中传递tempdata,c#,asp.net,asp.net-mvc,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 5,我需要在其中一个视图中传递一条注销成功消息,但我无法这样做。这是我的 无效解决方案: //LogController: public ActionResult Logoff() { DoLogOff(); TempData["Message"] = "Success"; return RedirectToAction("Index", "Home"); } // HomeController public ActionResult In

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

无效解决方案:

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

  // HomeController
  public ActionResult Index()
  {
      return View();
  }
@Html.Partial("../Home/DisplayPreview")
   @TempData["Message"] 
   @TempData["Message"] 
索引CSHTML文件:

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

  // HomeController
  public ActionResult Index()
  {
      return View();
  }
@Html.Partial("../Home/DisplayPreview")
   @TempData["Message"] 
   @TempData["Message"] 
显示预览CSHTML文件:

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

  // HomeController
  public ActionResult Index()
  {
      return View();
  }
@Html.Partial("../Home/DisplayPreview")
   @TempData["Message"] 
   @TempData["Message"] 
工作解决方案

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();
}
索引CSHTML文件:

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

  // HomeController
  public ActionResult Index()
  {
      return View();
  }
@Html.Partial("../Home/DisplayPreview")
   @TempData["Message"] 
   @TempData["Message"] 
但我想要我的第一个解决方案

看看这是否有效:

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

public ActionResult Index()
{
    ViewBag.Message = TempData["Message"];
    return View();
}
public ActionResult Logoff()
{
    DoLogOff();
    TempData["Message"] = "Success";
    return RedirectToAction("Index", "Home");
}
然后你可以在视图中使用它,比如

@ViewBag.Message

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


您可以尝试的只是向索引视图传递一个标志,该标志将显示已注销的消息(如果存在)。我不会使用字符串消息,就像您在“工作”示例中所显示的那样,因为攻击者可以利用该消息将用户重定向到恶意站点。

您是否混淆了“工作”和“不工作”解决方案?因为你所说的“不起作用”是唯一有效的方法,而你所说的“起作用”肯定不起作用。不,请再看一眼。他们在不同的控制者中。为什么投反对票?不确定。那次否决票不是我投的。不管怎样,第一种方法有什么问题?什么是无效的?您已经找到了有效的解决方案。为什么要关心
TempData
?所有依赖ASP.NET会话(以及服务器端状态)的操作都应该避免。是的,你是对的。我刚才看到了密码。我正在调用
会话。放弃
会话。在我的DolLogoff()中清除
。因此,您希望我在索引视图中将bool变量作为参数传递,而不是字符串?@Unbreakable-这是一种可能性,有许多其他方法可以做到这一点,这只是一个示例。你不想传递字符串,因为有人可能会在其中插入javascript,这会造成麻烦。如果你有时间,你可以把我应该做的事情的一小段代码放进去。
return RedirectToAction(action,homeController,new{showMes=“true”})如何从此处传递bool?在本地主机上调试时,使用redirecttoaction调用清除临时数据