开发者

Authorizing sections of a view in MVC

I was wondering if it's possible to authorize parts of a view inside the view.

For example, I understand how to authorize the entire controller in this method

<HandleError()> _
Public Class HomeController
  Inherits System.Web.Mvc.Controller

  Function Index()
    Return View()
  End Function

  <Authorize(Roles:="Administrators")> _
  Function AdministratorSecrets()
    Return View()
  End Function

End Class

But what Id like to do is have it so if the admin is logged in, they can see additional links in my navigation.

Something along the lines of

            <ul id="menu">              
                <li><%= Html.ActionLink("Home", "Index", "Home")%></li>
                <li><%= Html.ActionLink("About", "About", "Home")%></li>
             开发者_开发知识库   <Authorize(Roles:="Administrators")> _
                <li><%= Html.ActionLink("Admin", "Admin", "Home")%></li>
            </ul>

Obviously that won't work, but it gives an idea of what I'm trying to accomplish.

Any ideas?


Use something like this:

<% if(Roles.IsUserInRole("Administrator")){ %>
<span>HTML Code</span>
<% } %>


It's the best practice to send the if stuff to the new html helper extension method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜