Magnus Karlsson
Magnus Karlsson

Reputation: 3579

Make ActionLink to specific user

How do I make the links on the site lead to the current user's page(s)? I dont want them to get lists of every user but only themselves. I'm thinking something like this for my menu links:

  • @Html.ActionLink("My Details", "Details", "Customers", Membership.GetUser().ProviderUserKey)
  • Upvotes: 0

    Views: 163

    Answers (2)

    anAgent
    anAgent

    Reputation: 2780

    If you want the "Links" to display to show when the page is viewed by an authenticated user, create a ChildAction that returns the data you want like:

        [Authorize]
        [ChildActionOnly]
        public ActionResult UserLinks() {
            var items = someRepository.GetLinks(User.Identity.Name);
            return PartialView(items,"SomePartialView");
        }
    

    Then, use the RenderAction in the view like so:

    @{ Html.RenderAction("UserLinks");}
    

    Secondly, I agree with the comment by "Afshin Gh". Rather than passing data in your "ActionLink(s)", you could code it in your Controller to filter the data as required - like I'm showing in the "UserLinks" method above. This way, someone can't just manipulate the Url to display data for another customer.

    Upvotes: 1

    Afshin Gh
    Afshin Gh

    Reputation: 8198

    I think it's better if you send user to "My Details" page without any parameter or route values.

    The page is going to show current users data. So why to pass it like this?

    Just Redirect user to "My Detail" page, and after that, when user is in that page, you can get current user using: HttpContext.CurrentUser.

    Don't put user data in route values. Instead you can get it in that page's controller and pass it to the page.

    Upvotes: 3

    Related Questions