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 4618082
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:12:58+00:00 2026-05-22T02:12:58+00:00

In my MVC2 app that uses the Service – Repository pattern, how can I

  • 0

In my MVC2 app that uses the Service – Repository pattern, how can I call a service method from the master page?

+--------------------------------------+
| Logo                      Welcome xyz|
+--------------------------------------+
| Home | Sales | Import | Admin (menu) |
+--------------------------------------+

In my menu I now have some pages that have restricted access by user role. I have an existing service method that can check if the current user can view a certain page or not:

IPageAccessService.CanAccess(int pageId, int roleId);

On the controller methods I can call this to check if the user can see the page or not:

public ActionResult Update(int id?)
{
    if (!_pageAccessService.CanAccess(pageId, roleId))
    {
        return RedirectToAction("Index", "Home");
    }
}

But I don’t know how to call this method from my Site.Master so that when it creates the menu it does not show the menu item if the user does not have access (the menu is a simple unordered list):

<li><a href="<%=Url.Content("~/Admin") %>">Admin</a>
<ul>
    <li><a href="<%=Url.Content("~/Admin/Roles") %>">User Roles</a></li>
    <li><a href="<%=Url.Content("~/Admin/AdminReports") %>">Admin Reports</a></li>
</ul>
</li>

I’m guessing that it would need look something like this (have to check each page before adding to the list):

if (_pageAccessService.CanAccess(pageId, roleId)) <li><a href="<%=Url.Content("~/Admin") %>">Admin</a>
<ul>
        if (_pageAccessService.CanAccess(pageId, roleId)) <li><a href="<%=Url.Content("~/Admin/Roles") %>">User Roles</a></li>
        if (_pageAccessService.CanAccess(pageId, roleId)) <li><a href="<%=Url.Content("~/Admin/AdminReports") %>">Admin Reports</a></li>
</ul>
</li>

But before I can do that I need to know how to actually call a service method from the master.

EDIT:

I’ve adapted Darin’s answer and have this:

public static class LinkExtensions
{
    private static readonly IPageAccessRepository _repo = new PageAccessRepository();
    private static readonly IPageAccessService _pageAccess = new PageAccessService(_repo);

    public static MvcHtmlString MenuItem(
    this HtmlHelper htmlHelper, string linkText,
    string url, string pageName
    )
    {
        if (!_pageAccess.CanAccess(pageName))
        {
            return MvcHtmlString.Empty;
        }
        // The user can access the page => show the menu
        var a = new TagBuilder("a");
        a.Attributes["href"] = url;
        a.SetInnerText(linkText);
        return MvcHtmlString.Create(string.Format("<li>{0}</li>",a));
    }

The problem is that I still need to call the service, so I need to be able to instantiate it. Because its a static class, my IoC container won’t help here. So I still have to manually do create the service and repository. And it’s still got the same problem as my original ugly workaround – manually creating a repository in a view.

  • 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-22T02:12:58+00:00Added an answer on May 22, 2026 at 2:12 am

    You could write a custom HTML helper rendering the different items of this menu. Inside the helper based on the user roles you will decide whether to generate or not the given item. For example something among the lines:

    public static class LinkExtensions
    {
        public static MvcHtmlString MenuItem(
            this HtmlHelper htmlHelper,
            string linkText,
            string url,
            string requiredRole
        )
        {
            var a = new TagBuilder("a");
            a.Attributes["href"] = url;
            a.SetInnerText(linkText);
            if (string.IsNullOrEmpty(requiredRole))
            {
                // No role required => show the menu item
                return MvcHtmlString.Create(a.ToString());
            }
    
            var user = htmlHelper.ViewContext.HttpContext.User;
            if (!user.IsInRole(requiredRole))
            {
                // A role is required but no user authenticated or user is not in role
                // => show empty
                return MvcHtmlString.Empty;
            }
    
            // The user is in role => show the menu
            return MvcHtmlString.Create(a.ToString());
        }
    }
    

    and inside the view:

    <li>
        <%= Html.MenuItem("Admin", Url.Content("~/Admin"), "admin") %>
        <ul>
            <li>
                <%= Html.MenuItem("User Roles", Url.Content("~/Admin/Roles"), "userroles") %>
            </li>
            <li>
                <%= Html.MenuItem("Admin Reports", Url.Content("~/Admin/AdminReports"), "admin") %>
            </li>
        </ul>
    </li>
    

    Another possibility is to use child actions and the Html.Action helper inside the master.

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

Sidebar

Related Questions

I'm sending data from one page to the next in an MVC2 app. Page
I followed the instructions here Problem performing Ajax call from ASP.NET MVC2 app ,
My MVC2 app uses a component that makes subsequent AJAX calls back to the
I'm working with an MVC2 app that had migrated from an MVC1 app quite
I have an ASP.NET MVC2 application that uses a parent controller to setup specific
So I have an MVC 2 app that uses the Active Directory Membership Provider.
I have a simple MVC2 app that doesn't seem to Redirect correctly. The code
I am developing a commercial MVC2 app that requires a grid that has callback
I have a situation with my MVC2 app where I have multiple pages that
I have an ASP.NET MVC 2 app that uses URLS such as: /rsvp/33c4cf68-a2fe-4c0f-9834-08838e0532c3 /rsvp/4f28dad7-b05c-4887-818f-b4ae664b7192

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.