ASP.NET MVC 4 How do you serve different HTML based on Role?

后端 未结 3 555
滥情空心
滥情空心 2021-01-19 04:44

On top of allowing only certain Roles to access certain Controller or Action, I would like to serve a slightly different HTML based on the roles.

Admin can see Edit

3条回答
  •  梦谈多话
    2021-01-19 05:49

    If the view are quite similar than I usually handle this by creating a view that has the basic detail in that all the roles can see. I then typically add a few Partials for each role

     @Html.Partial("_AdminToolbar")
     @Html.Partial("_UserToolbar")
    

    These partials have a simple

    @if ( User.IsInRole("Admin") ) 
    

    to only show the partial for the role. This works best if the partial is going to be reused in lots of places and makes the view easier to read and saves you having to repeat code with almost identical views.

    You can always use the User.InRole or !User.InRole in the view to wrap bits of it to be shown or not shown.

    It's also helpful to remember that in more complex cases, the position that the final HTML gets placed in the web page is not always the position that its rendered into the content. CSS can be used to position the content in the desires location and the entire contents for the admin can be injected into the output in one partial or inside one User.InRole block.

    You could also use a shared base view for and then place a number of markers around different sections using RenderSection

    @RenderSection("AdminSection", required: false)
    

    Then in any specific views you have; there you can have a layout reference to the base view included in that section only if the page demands it because its only for an Admin. Then user's version wouldn't have the section. This helps to keep the code repeats/edits low or less.

提交回复
热议问题