【问题标题】:redirect to a actionresult in mvc but not want to go to page by return view()重定向到 mvc 中的 actionresult 但不想通过 return view() 转到页面
【发布时间】:2011-03-12 06:33:25
【问题描述】:

我想要一个工具,如果用户登录然后转到主页/索引

我不想用

return view('~/views/home/index');

我想将它重定向到我的 web 应用程序的主控制器的 actionresult 索引(在 asp.net mvc 中)

如何在没有返回视图()的情况下做到这一点;我想重定向他。

public actionresult login

if(userlogin)
{
// goes to index page ? what i do here then user goes to index page of home controller
}
else
{
return view()
}

【问题讨论】:

  • 顺便说一句,如果您使用 Visual Studio 中的项目模板创建了 MVC 项目,它应该已经为您生成了 Account 控制器。即使您选择不使用它,它也会展示您可以使用的类似内容。 (因为达林的回答正是它所做的......)

标签: asp.net-mvc


【解决方案1】:

你可以重定向:

return RedirectToAction("index");

如果动作在不同的控制器上,还要指定控制器名称:

return RedirectToAction("index", "home");

【讨论】:

  • 我使用了 return Redirect("/home/index");
  • 这不好,因为您正在硬编码路线 /home/index 并且在您修改路线规则的那一天它会咬你。
  • 另外,return Redirect("/home/index"); 在 vroot 中运行时会立即失败。至少在前面加上一个“~”,尽管正如 Darin 指出的那样,执行 RedirectToAction() 会好得多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 1970-01-01
  • 2013-02-26
  • 2016-05-04
  • 2011-01-15
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多