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

  • SEARCH
  • Home
  • 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 217461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:39:34+00:00 2026-05-11T18:39:34+00:00

I wish to display content depending on the given role(s) of the active user

  • 0

I wish to display content depending on the given role(s) of the active user , in the ASP.NET MVC.

Compare the old fashion way, using WebForms:

protected void Page_Load(Object sender, EventArgs e)
{
   if(User.IsInRole("Administrator")) {
       adminLink.Visible = true;
   }
}

Now how would I go on writing that when using the ASP.NET MVC ?
From my point of view, it would be wrong to place it directly in the View File, and assigning a variable for every single view won’t be pretty either.

  • 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-11T18:39:35+00:00Added an answer on May 11, 2026 at 6:39 pm

    Create Html helper and check current user roles in its code:

    public static class Html
    {
        public static string Admin(this HtmlHelper html)
        {
            var user = html.ViewContext.HttpContext.User;
    
            if (!user.IsInRole("Administrator")) {
                // display nothing
                return String.Empty;
    
                // or maybe another link ?
            }
    
            var a = new TagBuilder("a");
            a["href"] = "#";
            a.SetInnerText("Admin");
    
            var div = new TagBuilder("div") {
                InnerHtml = a.ToString(TagRenderMode.Normal);
            }
    
            return div.ToString(TagRenderMode.Normal);
        }
    }
    

    UPDATED:

    Or create wrapper for stock Html helper. Example for ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName):

    public static class Html
    {
        public static string RoleActionLink(this HtmlHelper html, string role, string linkText, string actionName, string controllerName)
        {
            return html.ViewContext.HttpContext.User.IsInRole(role)
                ? html.ActionLink(linkText, actionName, controllerName)
                : String.Empty;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two panels that i wish to display to the user. I decided
Under certain circumstances, I wish to display an error message to the user if
In a certain case I do not wish to display a the content of
I have a website with live stats that I wish to display to every
I wish to achive whenever an user clicks my link, only the footer will
I'm working on this tutorial(time is 33:55): http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ While what is shown at localhost:3000
I have a linq expression and I wish to display it in the log
I have a Rails site, where the content is written in markdown. I wish
I wish to decide what template (view) I should display based on the response
I've added the content from DirectoryInfo.GetFiles() to a List<string> . Now I wish to

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.