ASP.NET MVC在登录/注销时更改默认路由

Dan*_*rik 5 c# asp.net-mvc routing routes

我有一个非常简单的要求.

如果用户访问http://www.somedomain.com/并且未登录,我希望MVC将用户路由到HomeController.

如果用户去http://www.somedomain.com/和登录,我想MVC路由用户ClientController.

这个问题有一个简单的解决方案吗?

非常感谢你!

Mar*_*tin 3

在 HomeController 的 Index 操作中,如果 HttpContext.User 不为 null,则重定向到 ClientController:

public class HomeController : Controller
{

    public ActionResult Index()
    {
        if (HttpContext.User != null)
        {
            RedirectToAction("Index", "Client");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

编辑:或使用 Request.IsAuthenticated

public class HomeController : Controller
{

    public ActionResult Index()
    {
        if (Request.IsAuthenticated)
        {
            RedirectToAction("Index", "Client");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)