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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:18:34+00:00 2026-06-15T19:18:34+00:00

I’d like to maintain ASP.NET MVC 4’s existing controller/action/id routing with default controller =

  • 0

I’d like to maintain ASP.NET MVC 4’s existing controller/action/id routing with default controller = Home and default action = Index, but also enable controller/id to route to the controller’s index method as long as the second item is not a known action.

For example, given a controller Home with actions Index and Send:

/Home/Send -> controller's Send method
/Home -> controller's Index method
/Home/Send/xyz -> controller's Send method with id = xyz
/Home/abc -> controller's Index method with id = abc

However, if I define either route first, it hides the other one. How would I do this?

  • 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-15T19:18:35+00:00Added an answer on June 15, 2026 at 7:18 pm

    In case, that the list of your actions (e.g. Send) is well known, and their (action) names cannot be the same as some ID value, we can use our custom ConstraintImplementation:

    public class MyRouteConstraint : IRouteConstraint
    {
      public readonly IList<string> KnownActions = new List<string> 
           { "Send", "Find", ... }; // explicit action names
    
      public bool Match(System.Web.HttpContextBase httpContext, Route route
                      , string parameterName, RouteValueDictionary values
                      , RouteDirection routeDirection)
      {
        // for now skip the Url generation
        if (routeDirection.Equals(RouteDirection.UrlGeneration))
        {
          return false; // leave it on default
        }
    
        // try to find out our parameters
        string action = values["action"].ToString();
        string id = values["id"].ToString();
    
        // id and action were provided?
        var bothProvided = !(string.IsNullOrEmpty(action) || string.IsNullOrEmpty(id));
        if (bothProvided)
        {
          return false; // leave it on default
        }
    
        var isKnownAction = KnownActions.Contains(action
                               , StringComparer.InvariantCultureIgnoreCase);
    
        // action is known
        if (isKnownAction)
        {
          return false; // leave it on default
        }
    
        // action is not known, id was found
        values["action"] = "Index"; // change action
        values["id"] = action; // use the id
    
        return true;
      }
    

    And the route map (before the default one – both must be provided), should look like this:

    routes.MapRoute(
      name: "DefaultMap",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = string.Empty, action = "Index", id = string.Empty },
      constraints: new { lang = new MyRouteConstraint() }
    );
    

    Summary: In this case, we are evaluating the value of the “action” parameter.

    • if both 1) action and 2) id are provided, we won’t handle it here.
    • nor if this is known action (in the list, or reflected…).
    • only if the action name is unknown, let’s change the route values: set action to “Index” and action value to ID.

    NOTE: action names and id values need to be unique… then this will work

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

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms

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.