views:

33

answers:

1
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%
    if (Request.IsAuthenticated) {
%>
        Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>!
        [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ]
        <br /> 
<% if(User.IsInRole("Administrator")) { %>
        <br />
        <%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %> | <%= Html.ActionLink("UserControl","UserControl","Account")%>
        <% } else { %>
        <br />
        <%= Html.ActionLink("Home", "Index", "Home") %> | <%= Html.ActionLink("About", "About", "Home") %>
     <%} %>

I want to check the role of the user if it is an administrator only then the user can see the link to user control.when i try to run this code it is giving me an error saying "'Data.User' does not contain a definition for 'IsInRole'"

+3  A: 

I don't believe the ViewUserControl type has a User property, so the compiler thinks you're trying to access your domain object of type Data.User. In a partial control, you can access the User instance using HttpContext.Current.User where the IsInRole method should work.

Matthew Abbott