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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:57:55+00:00 2026-06-12T17:57:55+00:00

Is it possible to have the default value for a route parameter be another

  • 0

Is it possible to have the default value for a route parameter be another value from the route?

e.g. something like:

routes.MapRoute(null,
    "{controller}/{action}/{parentid}/{currentid}",
    new { controller = "Home", action = "Display", currentid = "{parentid}" }
);

parentid is compulsory and will always have a value. If no value has been specified for currentid I’d like it to take the value of parentid.

  • 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-06-12T17:57:57+00:00Added an answer on June 12, 2026 at 5:57 pm

    I don’t know if this functionality is supported out of the box but you could always write a custom route to implement it:

    public class MyRoute : Route
    {
        public MyRoute() :
            base(
                "{controller}/{action}/{parentid}/{currentid}",
                new RouteValueDictionary(new
                {
                    controller = "home",
                    action = "display",
                    currentid = UrlParameter.Optional
                }),
                new RouteValueDictionary(new
                {
                    // adjust the constraint if your ids are not numeric
                    // but you must have a constraint when a route parameter is
                    // non optional unless this is the last parameter
                    parentid = @"^([0-9])+$"
                }),
                new MvcRouteHandler()
        )
        {
        }
    
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var rd = base.GetRouteData(httpContext);
            if (rd == null)
            {
                return null;
            }
    
            var currentid = rd.Values["currentid"] as string;
            if (!string.IsNullOrEmpty(currentid))
            {
                return rd;
            }
    
            rd.Values["currentid"] = rd.Values["parentid"];
            return rd;
        }
    }
    

    and then register this route:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.Add("myroute", new MyRoute());
    }
    

    Now assuming you have defined the following controller:

    public class HomeController : Controller
    {
        public ActionResult Display(int parentid, int currentid)
        {
            return Content(string.Format("{0}, {1}", parentid, currentid));
        }
    }
    

    you could request it by:

    • /home/display/123 -> parentid=123 and currentid=123
    • /home/display/123/456 -> parentid=123 and currentid=456
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can I have a route like... routes.MapRoute( Boundaries-Show, Boundaries, new { controller = Boundaries,
I want to set up a ASP.NET MVC route that looks like: routes.MapRoute( Default,
I have these routes defined: routes.MapRoute(CategoryList_CountryLanguage, Categories/{id}/{urlCategoryName}/{country}/{language}, new { controller = Categories, action =
I have a uri scheme which is like <country>/<id>_<action>/<name> which descends from an old
Is it possible (via programming or xml configuration) to have a default value for
Possible Duplicate: Django models: default value for column I have created a model field
Short: Is there a way to have a route-definition that will pass the CONTROLLER/ACTION
Any Ideas? I asked the question "Is it possible to have a default parameter
Is it possible to have a HashMap return a default value for all keys
Possible Duplicate: passing an empty array as default value of optional parameter in c#

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.