mercredi 6 avril 2016

ASP.NET MVC profile routing

I am trying to come up with a profile page for my MVC application.

I have an Account Controller with a Profile() action like so:

public ActionResult Profile(String username, String page)
{
    // Get logged in users info
    User loggedInUser = new User();
    loggedInUser.Username = "loggedInUsername";

    // Display url users info
    // Search all Users to find user
    for(int i = 0; i < allUsers.Count; i++)
    {
        // Username is found
        if(allUsers.ElementAt(i).Username.Equals(username))
        {
            // Show different page/tab based on page parameter


            // Check if username is logged in or not
            if (loggedInUser.Username.Equals(username))
            {
                // If logged in, show more personal data
                return View(loggedInUser);
            }
            else
            {
                // If not logged in, show less personal data
                return View(allUsers.ElementAt(i));
            }
        }
    }
    // Username doesn't exist, redirect to error page
    User badUser = new Models.User();
    badUser.Username = "Bad User!";
    return View(badUser);
}

My routing:

routes.MapRoute(
    name: "Profile",
    url: "Account/Profile/{username}/{page}",
    defaults: new { controller = "Account", action = "Profile", username = "Admin", page = "Home" }
);

My question is how should I go about redirecting a user to their specific page (such as Home, Games, etc tabs)

Should I just hide other pages/tabs (div) and show the one based on the param "page" or is there a much better way of doing this.

I understand that my default username is "Admin" and page is "Home" which I want if they user doesn't enter anything but Account/Profile, but I was wondering what the standard way is to do this.

Aucun commentaire:

Enregistrer un commentaire