【问题标题】:How to redirect to another action or route? [duplicate]如何重定向到另一个动作或路线? [复制]
【发布时间】:2023-03-10 22:10:02
【问题描述】:

在我的 Razor 代码中,我有以下操作链接。

@Html.ActionLink("poof", "Poof", new { Id = Model.Id })

我想要发生的是调用 Poof 方法,但是我希望计算机导航到另一个视图 - 例如显示

public ActionResult Show(Guid id)
{
  return View(...);
}

public void Poof(Guid id)
{
  ...
  RedirectToRoute("Show", new {Id = id});
  RedirectToAction("Show", new {Id = id});
}

我尝试了 RedirectToRouteRedirectToAction,但在这两种情况下,我都进入了一个空白屏幕。仔细查看 URL 发现我被困在 .../Poof/... 路线而不是被引导到 .../Show/... 我不知道如何跳转到相应的链接。

我正在关注this answer 之类的内容,但由于 Show 操作中的断点未命中(即在重定向时),我开始怀疑它是否更适合使用this blog 之类的路线映射。

【问题讨论】:

  • 你应该使用*Action,除非你有特定的理由使用*Route

标签: c# asp.net-mvc razor routing actionlink


【解决方案1】:

即使Poof 正在重定向到另一个动作,它本身仍然是一个动作,因此它需要返回一个ActionResult。出于这个原因,RedirectToAction 返回一个ActionResult

public ActionResult Poof(Guid id)
{
    // ...

    return RedirectToAction("Show", new {Id = id});
}

【讨论】:

    猜你喜欢
    • 2018-12-18
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 2016-11-08
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多