如何重定向到另一个操作或路径?(复制)

时间:2022-10-30 18:07:16

This question already has an answer here:

这个问题已经有了答案:

In my Razor code I have the following action link.

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

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

What I want to happen is that the Poof method is invoked but then, I want the computer to navigate to another view - e.g. Show.

我希望发生的是调用Poof方法,然后,我希望计算机导航到另一个视图——例如Show。

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

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

I've tried both RedirectToRoute and RedirectToAction but in both cases, I landed at an empty screen. A closer look at the URL showed that I'm getting stuck at the .../Poof/... route instead of being ushered to .../Show/... and I can't figure out how to bounce on to the appropriate link.

我尝试过RedirectToRoute和RedirectToAction,但在这两种情况下,我都是在一个空屏幕上着陆的。仔细看一下URL就会发现我被困在…/Poof/…引导而不是被引导到……我也不知道如何找到合适的链接。

I'm following something like this answer but since the break-point in Show action isn't hit on (on redirect, that is), I start to wonder if it's more appropriate to play around with route mapping like this blog.

我正在遵循这样的答案,但是由于Show action中的断点没有被打开(也就是重定向),我开始怀疑像这个博客一样使用路由映射是否更合适。

1 个解决方案

#1


1  

Even though Poof is redirecting to another action, it is still an action itself and as such it needs to return an ActionResult. RedirectToAction returns an ActionResult for just this reason.

尽管Poof正在重定向到另一个动作,但它本身仍然是一个动作,因此它需要返回一个动作结果。RedirectToAction为此返回一个ActionResult。

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

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

#1


1  

Even though Poof is redirecting to another action, it is still an action itself and as such it needs to return an ActionResult. RedirectToAction returns an ActionResult for just this reason.

尽管Poof正在重定向到另一个动作,但它本身仍然是一个动作,因此它需要返回一个动作结果。RedirectToAction为此返回一个ActionResult。

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

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