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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:37:31+00:00 2026-05-29T19:37:31+00:00

From inside a mvc (2) user control, I want to loop through all the

  • 0

From inside a mvc (2) user control, I want to loop through all the route values.

So if I have controllers like:

UserController
AccountController

I need a collection of the values that will appear in the url like:

/user/...
/account/...

i.e. the values user, account.

How can I get this?

I tried RouteTables but couldn’t figure it out.

  • 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-29T19:37:32+00:00Added an answer on May 29, 2026 at 7:37 pm

    Oh, really a good question to keep my self busy for an hour.
    To achieve the required functionality , we need to hook into MVC source and little bit of reflections.

    1. By default Route names are not available , so we need to write a Route collection extension to save Route Name in RouteData tokens.

      public static Route MapRouteWithName(this RouteCollection routes,string name, string   url, object defaults=null, object constraints=null)
      {
      
      Route route = routes.MapRoute(name, url, defaults, constraints);
      route.DataTokens = new RouteValueDictionary();
      route.DataTokens.Add("RouteName", name);
      return route;
      }
      
    2. Modify the global.asax maproute call to invoke previous extension

      routes.MapRouteWithName(
                  "Default", // Route name
                  "{controller}/{action}/{id}", // URL with parameters
                  new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
              );
      
    3. Modified the MVC PathHelper a little bit.(Include this helper in the project)

      using System;
      using System.Collections.Specialized;
      using System.Web;
      
      public static class PathHelpers
      {
      
      // this method can accept an app-relative path or an absolute path for contentPath
      public static string GenerateClientUrl(HttpContextBase httpContext, string contentPath)
      {
          if (String.IsNullOrEmpty(contentPath))
          {
              return contentPath;
          }
      
          // many of the methods we call internally can't handle query strings properly, so just strip it out for
          // the time being
          string query;
          contentPath = StripQuery(contentPath, out query);
      
          return GenerateClientUrlInternal(httpContext, contentPath) + query;
      }
      
      private static string GenerateClientUrlInternal(HttpContextBase httpContext, string contentPath)
      {
          if (String.IsNullOrEmpty(contentPath))
          {
              return contentPath;
          }
      
          // can't call VirtualPathUtility.IsAppRelative since it throws on some inputs
          bool isAppRelative = contentPath[0] == '~';
          if (isAppRelative)
          {
              string absoluteContentPath = VirtualPathUtility.ToAbsolute(contentPath, httpContext.Request.ApplicationPath);
              string modifiedAbsoluteContentPath = httpContext.Response.ApplyAppPathModifier(absoluteContentPath);
              return GenerateClientUrlInternal(httpContext, modifiedAbsoluteContentPath);
          }
      
          string relativeUrlToDestination = MakeRelative(httpContext.Request.Path, contentPath);
          string absoluteUrlToDestination = MakeAbsolute(httpContext.Request.RawUrl, relativeUrlToDestination);
          return absoluteUrlToDestination;
      }
      
      public static string MakeAbsolute(string basePath, string relativePath)
      {
          // The Combine() method can't handle query strings on the base path, so we trim it off.
          string query;
          basePath = StripQuery(basePath, out query);
          return VirtualPathUtility.Combine(basePath, relativePath);
      }
      
      public static string MakeRelative(string fromPath, string toPath)
      {
          string relativeUrl = VirtualPathUtility.MakeRelative(fromPath, toPath);
          if (String.IsNullOrEmpty(relativeUrl) || relativeUrl[0] == '?')
          {
              // Sometimes VirtualPathUtility.MakeRelative() will return an empty string when it meant to return '.',
              // but links to {empty string} are browser dependent. We replace it with an explicit path to force
              // consistency across browsers.
              relativeUrl = "./" + relativeUrl;
          }
          return relativeUrl;
      }
      
      private static string StripQuery(string path, out string query)
      {
          int queryIndex = path.IndexOf('?');
          if (queryIndex >= 0)
          {
              query = path.Substring(queryIndex);
              return path.Substring(0, queryIndex);
          }
          else
          {
              query = null;
              return path;
          }
      }
      
      }
      
    4. Add few Helper methods in controller

      public static string GenerateUrl(string routeName, string actionName, string controllerName, RouteCollection routeCollection, RequestContext requestContext)
      {
      
          RouteValueDictionary mergedRouteValues = MergeRouteValues(actionName, controllerName);
      
          VirtualPathData vpd = routeCollection.GetVirtualPathForArea(requestContext, routeName, mergedRouteValues);
          if (vpd == null)
          {
              return null;
          }
      
          string modifiedUrl = PathHelpers.GenerateClientUrl(requestContext.HttpContext, vpd.VirtualPath);
          return modifiedUrl;
      }
      public static RouteValueDictionary MergeRouteValues(string actionName, string controllerName)
      {
          // Create a new dictionary containing implicit and auto-generated values
          RouteValueDictionary mergedRouteValues = new RouteValueDictionary();
      
          // Merge explicit parameters when not null
          if (actionName != null)
          {
              mergedRouteValues["action"] = actionName;
          }
      
          if (controllerName != null)
          {
              mergedRouteValues["controller"] = controllerName;
          }
      
          return mergedRouteValues;
      }
      
    5. Now we can write some reflection logics to read controllers, actions and routenames.

      Dictionary<string, List<string>> controllersAndActions = new Dictionary<string, List<string>>();
      
      // Get all the controllers
      var controllers = Assembly.GetExecutingAssembly().GetTypes().Where(t => typeof(Controller).IsAssignableFrom(t));
      
      foreach (var controller in controllers)
      {
          List<string> actions = new List<string>();
          //Get all methods without HttpPost and with return type action result
          var methods = controller.GetMethods().Where(m => typeof(ActionResult).IsAssignableFrom(m.ReturnType)).Where(a=>!a.GetCustomAttributes(typeof(HttpPostAttribute),true).Any());
          methods.ToList().ForEach(a => {
              actions.Add(a.Name);
          });
          var controllerName = controller.Name;
          if (controllerName.EndsWith("Controller"))
          {
              var nameLength = controllerName.Length - "Controller".Length;
              controllerName = controllerName.Substring(0, nameLength);
          }
          controllersAndActions.Add(controllerName, actions);
      }
      List<string> allowedRoutes = new List<string>();
      
      var routeNames = RouteTable.Routes.Where(o=>o.GetRouteData(this.HttpContext)!=null).Select(r=>r.GetRouteData(this.HttpContext).DataTokens["RouteName"].ToString());
      foreach (var cName in controllersAndActions)
      {
          foreach (var aName in cName.Value)
          {
              foreach (var item in routeNames)
              {
                  allowedRoutes.Add(GenerateUrl(item, aName, cName.Key, RouteTable.Routes, this.Request.RequestContext));
              }
          }
      
      }
      
    6. Points to remember :If in the route you have defined any default parameters, then url for those controller and action will be empty. e.g. in above example "/Home/Index" will be shown as "/"

    7. Download the sample application Link To Download

      List item

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

Sidebar

Related Questions

I have Hibernate Transaction manager configured inside Spring MVC controller. <bean id=dataSource class=org.apache.commons.dbcp.BasicDataSource destroy-method=close>
I have a custom MVC application/framework where each action is a function inside of
Basically I keep getting thrown out from my asp.net mvc application because the User.Identity.IsAuthenticated
I have an ASP.NET MVC 3.0 application. Inside the application, I have a Login
I have seen a nice example whereby the MVC controller class inherits from another
From inside of the fireAlert() function, how can I reference the element that called
How do I call shell commands from inside of a Ruby program? How do
I'm firing off a Java application from inside of a C# .NET console application.
If you throw an exception from inside an MFC dialog, the app hangs, even
Given the following code snippet from inside a method; NSBezierPath * tempPath = [NSBezierPath

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.