ASP.NET MVC 5 Customise Bootstrap navbar based on User Role

前端 未结 3 1945
耶瑟儿~
耶瑟儿~ 2021-02-19 20:44

I\'m using the ASP.NET MVC 5 built in authentication methods. I would like to show and hide links (in the menu navbar) based on the role the user is in.

Has anyone ache

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 21:34

    Two things I do. Either

    User.IsInRole(admin)
    {link somewhere}
    

    Or what I personally do is because I use areas I have a viewstart in area admin which links to admin shared viewmodel then in admin shared view that links to the public view.

    In the admin shared view. I set up a section. Inside this section I define extra nav details what that specific role will see and add them in a list tag

    Then inside public shared view I then use (on phone can't remember exact name something like)
    Html.IsSectionDefined

    I personally like the second method using areas and sections both would work fine but with the second I find it much cleaner and you can be so much more specific and much simpler

提交回复
热议问题