【发布时间】:2017-09-29 03:54:22
【问题描述】:
我们正在使用具有“索引”操作的控制器:
[Route("")]
[Route("Index/{id:int?}")]
[Route("{id:int?}")]
public ActionResult Index(int? id)
{
var viewModel = new GroupViewModel();
....
return View("Index", viewModel);
}
我们可以通过使用example.com/my/ 或example.com/my/index 或example.com/my/index/1 来执行此操作。这可以按我们的意愿工作。现在我们想使用example.com/my/index/1 语法重定向到这里。
当我们执行这一行时:
return RedirectToAction("Index", "My", new { id = 3 });
它将使用此 url 重定向:
example.com/my?id=3
我们希望它改用example.com/my/index/1。
有没有人知道强制RedirectToAction 使用这个不带问号的约定的方法?
2017 年 5 月 2 日更新,以更正以下每个 cmets 的控制器名称
【问题讨论】:
-
Controller 是你的控制器的真实名称吗?
-
没有。抱歉,我在上面的示例中将名称更新为 MyController。
-
假设 MyController 是你的真实控制器名称,它应该是
return RedirectToAction("Index", "My", new { id = 3 }); -
我已根据您的 cmets 更新了控制器的名称。对困惑感到抱歉。感谢 CodeNotFound 和 Brian
标签: c# asp.net-mvc asp.net-mvc-routing redirecttoaction