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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:14:40+00:00 2026-06-11T15:14:40+00:00

I want to define a route with a constraint so that it only matches

  • 0

I want to define a route with a constraint so that it only matches known controller names. This is to allow me to define a fallback route for other URLs of the same form.

Specifically:

/User

Should take me to the Index action of the User controller (which exists)

/History

Should take me to the Index action of the History controller (which exists)

/es

As no “es” controller exists, should use the fallback route and take me to the Index action of the Home controller with a language parameter value of “es”.

I need this because I have a requirement to provide a special URL with a language code to launch the app in a given language. So there is now a need to distinguish between valid controller names and language names in the routing.

How can I implement RegisterRoutes to achieve this? Many thanks!

Edit: I realize that I can define a specific route for each of my controllers, which is OK (I don’t have a zillion controllers). But I’m wondering if I can rely on a generic constraint to achieve this so that I don’t have to define individual routes.

  • 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-11T15:14:41+00:00Added an answer on June 11, 2026 at 3:14 pm

    When defining your routes, use the “constraints” parameter to handle this.

    For instance:

    RouteTable.Routes.Add(new Route
    {
        Url = "{controller}/{action}",
        Constraints = new { controller = "User|History" },
        Defaults = new { action = "Index" }
    };
    
    RouteTable.Routes.Add(new Route
    {
        Url = "{languageCode}",
        Defaults = new { controller = "Home", action = "Index" }
    };
    

    The first route added will be evaluated first. If the token for “controller” is not matched against the constraints, the next route added will be evaluated and resolve to the Home controller with the Index action and the language token in the parameter languageCode. You might want to add a constraint to the languageCode token as well, to make sure only valid languages are matched. Then you could possibly add a third route as a catch-all route.

    You could also use a routehandler to deal with the languageCode as I have described in my blogpost Localization and MVC.

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

Sidebar

Related Questions

I want to define a class that supports __getitem__ , but does not allow
I want to define a route that have 2 optional parameters in the middle
Currently I have a route that looks like this: resources :posts I want to
At first sorry my bad English. I want define routing for this url: For
I want to define the data.store that store companyList, because I want to reuse
I want to define a class that is able to populate itself reading from
I want to define functions that can be used in the fixedpoint rule. For
I want to define a global before filter that will run before every request
Is it possible to define a route in MVC that dynamically resolves the action
I'm trying to define a route configuration which will allow an optional 'region' in

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.