Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7186609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:40:01+00:00 2026-05-28T18:40:01+00:00

I will have many Roles, and each Role has many functions, so the RequireRoles

  • 0

I will have many Roles, and each Role has many functions, so the RequireRoles Attribute I don’t think will suffice in my case. I need some way to dynamically let the Controller action define to the View what sections and/or controls in the View (without adding if/else logic inside the View).

My thought is that the Controller should be telling the View how to present itself and not the View with the if/else logic.

Any ideas on how to design this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:40:02+00:00Added an answer on May 28, 2026 at 6:40 pm

    You need to first of all create a filter which you can use an attribute to control what roles see what actions. See http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs.

    public class RequiresRoleAttribute : ActionFilterAttribute {
        private List<string> requiredRoles = null;
    
        /// <summary>
        /// Initializes a new instance of the <see cref="RequiresRoleAttribute"/> class.
        /// </summary>
        /// <param name="roleNames">The role names.</param>
        public RequiresRoleAttribute(params string[] roleNames) {
            this.requiredRoles = new List<string>(roleNames);
        }
    
        /// <summary>
        /// Called by the MVC framework before the action method executes.
        /// </summary>
        /// <param name="filterContext">The filter context.</param>
        public override void OnActionExecuting(ActionExecutingContext filterContext) {
            bool hasRole = false;
    
            // check to see if the user has the proper role here
    
            // if the do not have the role, they are not allowed to execute the action
            if (!hasRole)
                throw new UserAccessException("You do not have access to this action (" + filterContext.ActionDescriptor.ActionName + ", " + filterContext.ActionDescriptor.ControllerDescriptor.ControllerName + ")");
    
            base.OnActionExecuting(filterContext);
        }
    }
    

    Second to solve your problem of no logic in the views you could use child actions for each section which requires a role. Again you could apply your filter to the child actions. For more on child actions see: http://msdn.microsoft.com/en-us/library/ie/ee839451.aspx.

    What you would need to change is the section that throws the exception. You’d need to check to see if the action being executed is a child action. If so, you’d want to return an empty content result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a large project (for me) which will have many classes and
I have a form which have many check boxes in it. User will check
I have a model Task which will HABTM many TaskTargets. When it comes to
For a food online-ordering application, I have worked out how many ingridients we need
I will have multiple tables used by different projects on the same mySql server.
I have a table that potentially will have high number of inserts per second,
I have a script (or more accurately WILL have a script) that checks a
I have a scenario where a user will have access to a one-time-url. When
I have a UIView whose layers will have sublayers. I'd like to assign delegates
I have an app that will have entries of both varchar(max) and varbinary(max) data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.