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 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 am writing a Asp.net MVC 2 application that uses Forms Authentication and currently
I've disabled sessionState in my mvc2 app via the web.config and also created my
I am working on a project which uses a relational database (SQL Server 2008).
I found this question Can't use relative paths with areas in ASP.NET MVC 2
I've seen SO MANY articles that explain how to do this with IIS6, and
Currently working on a project that is being developed in VS2010. I'm running Windows
We are using Hibernate 3.1 with Spring MVC 2.0. Our problem occurs when data
I plan to have 2 projects. 1. mbtech.crm.mvc 2. mbtech.crm.tests I will have a
Per excellent advice I received on a recent question ( Database design problem ),
So I am basically looking for ideas here. I've never really had to do

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.