Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC one route, two different views

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!

like image 545
Pure.Krome Avatar asked Nov 11 '08 23:11

Pure.Krome


2 Answers

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");
    }
like image 63
Adrian Clark Avatar answered Sep 22 '22 11:09

Adrian Clark


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.

like image 23
taelor Avatar answered Sep 24 '22 11:09

taelor