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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:11:32+00:00 2026-06-15T01:11:32+00:00

As a followup of this question , I’m trying to implement a custom route

  • 0

As a followup of this question, I’m trying to implement a custom route constraint in order to dynamically modify the RouteValueDictionary for a specific route.

I’m 95% of the way there: I can match any route based on the supplied parameter pattern matching the action specified in the url. The very last step of this process is to overwrite the RouteValueDictionary to rename the keys as the appropriate parameters for the controller action.

Here’s the relevant snippet of my code (the entire class is almost 300 lines so I don’t want to add all of it, but I can if needed):

public class CustomRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string paramName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        if (routeDirection.Equals(RouteDirection.UrlGeneration)) {
            return false;
        }

        //this will grab all of the "actual" parameters out of the RVD, excluding action and controller 
        Dictionary<string, object> unMappedList = values.Where(x => x.Key.Contains("param")).OrderBy(xi => xi.Key).ToDictionary(
            kvp => kvp.Key, kvp => kvp.Value);

        string controller = values["controller"] as string;
        string action = values["action"] as string;

        //this method tries to find the controller using reflection
        Type cont = TryFindController(controller);

        if (cont != null) {
            MethodInfo actionMethod = cont.GetMethod(action);

            if (actionMethod != null) {
                ParameterInfo[] methodParameters = actionMethod.GetParameters();

                //this method validates that the parameters of the action that was found match the expected parameters passed in the custom constraint; it also performs type conversion
                if (validateParameters(methodParameters, unMappedList)) {
                    for (int i = 0; i < methodParameters.Length; i++) {
                        values.First(x => x.Key == unMappedList.ElementAt(i).Key).Key = methodParameters.ElementAt(i).Name; 
                        //above doesn't work, but even if it did it wouldn't do the right thing

                        return true;
                    }
                }
            }
        }

        return false;
    }
}

And here’s how I use the custom constraint:

routes.MapRoute(
    name: "Default2",
    url: "{controller}/{action}/{param1}-{param2}",
    defaults: new { controller = "Admin", action = "Index" },
    constraints: new { lang = new CustomRouteConstraint(new RoutePatternCollection( new List<ParamType> { ParamType.INT, ParamType.INT })) }
);

(What I’m passing into the constructor is basically saying “The parameter pattern to look for is two integers”. So when the Match method is called, it will return true if the action specified in the url has two two integer parameters, regardless of the actual parameter names.)

So, is there a way I can overwrite the RouteValueDictionary for this request? Or is there some other way I can do this that I’m missing?

  • 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-15T01:11:33+00:00Added an answer on June 15, 2026 at 1:11 am

    If I do understand correctly what is the question:

    So, is there a way I can overwrite the RouteValueDictionary for this
    request? Or is there some other way I can do this that I’m missing?

    I’ve taken your solution and changed these lines to replace parameter names:

    for (int i = 0; i < methodParameters.Length; i++)
    {
        // I. instead of this
    
        //values.First(x => x.Key == unMappedList.ElementAt(i).Key)
        //    .Key = methodParameters.ElementAt(i).Name; 
        //above doesn't work, but even if it did it wouldn't do the right thing
    
        // II. do this (not so elegant but working;)
        var key = unMappedList.ElementAt(i).Key;
        var value = values[key];
        values.Remove(key);
        values.Add(methodParameters.ElementAt(i).Name, value);
    
        // III. THIS is essential! if statement must be moved after the for loop
        //return true;
    }
    return true; // here we can return true
    

    NOTE: I like your approach. Sometimes it is simply much more important to extend/adjust routing then go to code and “fix incorrect parameter names”. And of course, they are most likely NOT incorrect.

    And here is the power of Separation of concern.

    • Url addresses are one place.
    • Controller, action and parameter names other one.
    • And Routing with powerful customization…

    …as the only correct place where to handle that. This is separation of concern. Not tweaking the action names to serve Url…

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

Sidebar

Related Questions

This is a followup question to: SQL Query with ORDER BY But I think
this is a followup question to Remove default apps from Django-admin I am trying
This is a followup to this question. Here's the code I'm trying to understand
Its a followup question of this . I'm trying to wrap the selected text
As a followup to this question , is it possible to write a single
As a followup to this two questions and its answers Question 1 and Question
NOTE: This is a followup to my question here. I have a program that
This is a followup on the question: ASP.NET next/previous buttons to display single row
This is a followup question to my other widget-related question . I'd like to
This is a followup question to my other question : Run bat file 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.