[RESOLVED]Routing : What paramater name are const and what parameter name can be changed ?

For example:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

When we are speaking about defaults

I tried to understand the main idea of url rule.

1. As I can understood everything you include in bracket is variable which can be included in defaults.

2. And also threre are somehing like thath {*url} its match everything string

Ok then I tried to understood when we set defaults what are the name of parameter should be ?

1. The name of the parameter should be as you set in url rule in braket. For example here I set {id} then I should use id = …

If I set url rule

{reporttype}/{year}/{month}/{day}

The I should use retportype, year, month, day as a parameter in default rules.

Ability to change controller and action name

Then I began to try to change control and action name

routes.MapRoute(
                name: "Default",
                url: "{contr}/{act}/{id}",
                defaults: new { contr = "Home", act = "Index", id = UrlParameter.Optional }
            );

And in this case contr is not recognize as controller and act is not recognize as action. They are understanded as parameters.

From that I made a conculusion that if you want to set controller name and action name you always should use
controller and action name for them in deafult.

So contoller and action name is const. And other parameter can be setted by yourself. Using {name} in rule.

I read everything I could find and I coludn’t find answer on my question.

If I have mistake please corect me.

The main quesion, is controller and action name const ??? Because it hard to understand that because it is a dynamic object.

The routemap is used to convert a url to a routevalues collection. The parameter names in the routemap are used to define the keys in the route collection. The routemap doesn’t care what you call these keys but other code does. The "area", "controller", and
"action" keys are used to find an action. Other keys are used to bind to the action parameters, and others are used for binding.

SmartWhy

So contoller and action name is const.

How would MVC know which controller and action to use for your routing. It knows by the naming convention. So in your routing, it searches for the name controller and action.

I believe you can override this behavior by writing your own route handler.

Leave a Reply