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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:15:08+00:00 2026-05-31T12:15:08+00:00

I’m working on an ASP.NET MVC solution that has a number of different menus.

  • 0

I’m working on an ASP.NET MVC solution that has a number of different menus. The menu to display depends on the role of the currently logged in user.

In MVC 3 I had some custom code to support this scenario, by having a single controller method that would return the right menu. It would do this by deferring the request to the appropriate controller and action depending on the current user.

This code appears to be broken in MVC 4 and I’m looking for help to fix it.

First, I added a TransferResult helper class to perform the redirection:

public class TransferResult : RedirectResult
{
    #region Transfer to URL
    public TransferResult( string url ) : base( url )
    {
    }
    #endregion

    #region Transfer using RouteValues
    public TransferResult( object routeValues ) : base( GetRouteUrl( routeValues ) )
    {
    }

    private static string GetRouteUrl( object routeValues )
    {
        var url = new UrlHelper( new RequestContext( new HttpContextWrapper( HttpContext.Current ), new RouteData() ), RouteTable.Routes );
        return url.RouteUrl( routeValues );
    }
    #endregion

    #region Transfer using ActionResult (T4MVC only)
    public TransferResult( ActionResult result ) : base( GetRouteUrl( result.GetT4MVCResult() ) )
    {
    }

    private static string GetRouteUrl( IT4MVCActionResult result )
    {
        var url = new UrlHelper( new RequestContext( new HttpContextWrapper( HttpContext.Current ), new RouteData() ), RouteTable.Routes );
        return url.RouteUrl( result.RouteValueDictionary );
    }
    #endregion

    public override void ExecuteResult( ControllerContext context )
    {
        HttpContext httpContext = HttpContext.Current;
        httpContext.RewritePath( Url, false );
        IHttpHandler httpHandler = new MvcHttpHandler();
        httpHandler.ProcessRequest( HttpContext.Current );
    }
}

Second, I modified T4MVC to emit a few controller helper methods, resulting in every controller having this method:

protected TransferResult Transfer( ActionResult result )
{
    return new TransferResult( result );
}

This allowed me to have a shared controller action to return a menu, without having to clutter the views with any conditional logic:

public virtual ActionResult Menu()
{
    if( Principal.IsInRole( Roles.Administrator ) )
        return Transfer( MVC.Admin.Actions.Menu() );
    return View( MVC.Home.Views.Partials.Menu );
}

However, the code in ExecuteResult in the TransferResult class does not seem to work with the current preview release of MVC 4. It gives me the following error (pointing to the “httpHandler.ProcessRequest” line):

'HttpContext.SetSessionStateBehavior' can only be invoked before
'HttpApplication.AcquireRequestState' event is raised.

Any idea how to fix this?

PS: I realize that I could achieve the same using a simple HtmlHelper extension, which is what I’m currently using as a workaround. However, I have many other scenarios where this method has allowed me to mix and reuse actions, and I would hate to give up this flexibility when moving to MVC 4.

  • 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-31T12:15:10+00:00Added an answer on May 31, 2026 at 12:15 pm

    Sometimes I think “MVC” should be called “RCMV” for “Router Controller Model View” since that is really the order that things happen. Also, since it is just “MVC”, people always tend to forget about routing. The great thing about MVC is that routing configurable and extensible. I believe what you are trying to do could be solved with a custom route handler.

    I haven’t tested this, but you should be able to do something like this:

    routes.Add(
       new Route(
           "{controller}/{action}/{id}", 
           new RouteValueDictionary(new { controller = "Home", action = "Menu" }), 
           new MyRouteHandler(Roles.Administrator, new { controller = "Admin" })));
    

    Then your route handler would look like this:

    public class MyRouteHandler : IRouteHandler
    {
        public string Role { get; set; }
    
        public object RouteValues { get; set; }
    
        public MyRouteHandler(string role, object routeValues)
        {
            Role = role;
            RouteValues = routeValues;
        }
    
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new MyHttpHandler(Role, RouteValues);
        }
    }
    

    And finally handle the re-routing in your HttpHandler:

    public class MyHttpHandler : IHttpHandler
    {
        public string Role { get; set; }
    
        public object RouteValues { get; set; }
    
        public MyHttpHandler(string role, object routeValues)
        {
            Role = role;
            RouteValues = routeValues;
        }
    
        public void ProcessRequest(HttpContext httpContext)
        {
            if (httpContext.User.IsInRole(Role))
            {
                RouteValueDictionary routeValues = new RouteValueDictionary(RouteValues);
    
                // put logic here to create path similar to what you were doing
                // before but you will need to replace any keys in your route 
                // with the values from the dictionary created above.
    
                httpContext.RewritePath(path);
            }
    
            IHttpHandler handler = new MvcHttpHandler();
            handler.ProcessRequest(httpContext);
        }
    }
    

    That may not be 100% correct, but it should get you in the right direction in a way that shouldn’t run into anything deprecated in MVC4.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a small JavaScript validation script that validates inputs based on Regex. I

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.