D-W
D-W

Reputation: 5341

Creating a Custom Attribute ASP.NET MVC

Looking for some advice, I have a bool property value on a user object that indicates if the user is an admin. I have some menu links on my view, which if your an admin I want to show(other wise dont show), I also want to ensure that the controller has an attribute that checks to ensure the user is an admin, so whats the best way to implement this, examples welcomed.

Upvotes: 1

Views: 2269

Answers (1)

maxs87
maxs87

Reputation: 2284

Here is my attribute for navigation build

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class NavigationItemAttribute : System.Attribute
{
    public NavigationItemAttribute(string text)
    {
        Text = text;
        SortOrder = int.MaxValue;
        ActionName = "Index";
        Category = Category;
        IconClass = IconClass;
        Domain = Domain;
    }

    public string Text { get; private set; }
    public string Area { get; set; }
    public int SortOrder { get; set; }
    public string ActionName { get; set; }
    public string Category { get; set; }
    public string IconClass { get; set; }
    public string Domain { get; set; }
}

and usage

[NavigationItem("!lang:navigation:admin_main!", SortOrder = 6, Domain = "main", IconClass = "icon-user")]
public class MembershipController : Controller

but roles using anyvay to show or not show item

Upvotes: 2

Related Questions