1110
1110

Reputation: 6839

Is this good way to handle page content view when user is not authenticated

When I run my asp mvc site first page that user see is Index from HomeController.

http://localhost:50975/

On that page displayed data belong only to that user.
When I logout user I want to my URL stay the same (in reality www.site.com):

http://localhost:50975/

But without content (because user is not logged in).
I handle this like this now but I wonder is it good way or there is better (probably there is).
In my view:

@if (Request.IsAuthenticated)
{... display data 
}
else{... show only some logo and login and register form

I don't know if I am clear enough with what I want, basically I want that URL stay same when user is at home page when user is authenticated and not authenticated.

Upvotes: 2

Views: 86

Answers (2)

orjan
orjan

Reputation: 1482

It also possible to return 2 different views from the controller. One if you're authenticated and one if you're not. Depending on how much the two views will differ.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1039398

Yes, this seems fine to be handled in your view and include different sections/partials based on whether the user is authenticated or not.

But if you find yourself repeating those ifs hundreds of times in your view transforming it into spaghetti code you could have 2 different views and handle the situation in your Index action:

public ActionResult Index()
{
    if (Request.IsAuthenticated)
    {
        return View("AuthenticatedIndex");
    }

    return View();
}

Upvotes: 3

Related Questions