views:

355

answers:

2

Hi Folks,

I'm trying to design a homepage for an MVC site that has two different views, based on if the user is logged in or not.

So image the default (not logged in) view is showing general, nonspecific info. If i'm logged in, the view is showing mostly personal stuff instead.

What's the best practice to handling this? Don't forget, we also need to unit test this.

Thanks heaps!

+10  A: 

This should be a simple case of returning the appropriate view from your controller.

public ActionResult Index()

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

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

        return View("AnonymousIndex");
    }
Aydsman
Serious? damn - that is simple! I was thinking of one view with split logic (which was scaring the hell out of me). Nice and clean. Awesome!
Pure.Krome
+1  A: 

I'm not sure if you could do

User.IsloggedOn

in the past, but now I have to say

User.Identity.IsAuthenticated

if you are using the Built In Web Forms Authentication.

taelor