重定向到mvc中的actionresult但不想通过返回视图转到页面()

9 asp.net-mvc

我想要一个设施,如果用户登录然后转到家/索引

我不想用

return view('~/views/home/index');
Run Code Online (Sandbox Code Playgroud)

我想将它重定向到我的web应用程序的home controller的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()
}
Run Code Online (Sandbox Code Playgroud)

Dar*_*rov 25

你可以重定向:

return RedirectToAction("index");
Run Code Online (Sandbox Code Playgroud)

如果操作在不同的控制器上,也指定控制器名称:

return RedirectToAction("index", "home");
Run Code Online (Sandbox Code Playgroud)

  • 这是不好的,因为你正在硬编码路由`/ home/index`,当你修改你的路由规则它会咬你. (2认同)
  • 另外,*返回Redirect("/ home/index");*只要你在vroot中运行就会失败.至少在前面添加一个"〜",尽管正如Darin指出的那样做*RedirectToAction()*要好得多. (2认同)