7

我正在尝试根据用户是否登录为具有两个不同视图的 MVC 站点设计主页。

因此,图像默认(未登录)视图显示一般的、非特定的信息。如果我已登录,则视图主要显示个人内容。

处理此问题的最佳做法是什么?别忘了,我们还需要对此进行单元测试。

多谢!

4

2 回答 2

13

这应该是从控制器返回适当视图的简单案例。

public ActionResult Index()

    If (User.IsLoggedOn)
    {
        // Do user-specific controller stuff here...

        return View("LoggedOnIndex");
    }
    else
    {
        // Do anon controller stuff here...

        return View("AnonymousIndex");
    }
于 2008-11-12T00:02:45.253 回答
2

我不确定你能不能做到

User.IsloggedOn

过去,但现在我不得不说

User.Identity.IsAuthenticated

如果您使用的是内置 Web 表单身份验证。

于 2009-03-24T20:16:41.627 回答