Action parameters are passed into the action method by Value Providers. These providers work by supplying the action method parameters when the key (the name of the parameter) matches a key in the value source. There are value providers for both route values and for query strings, but regardless of where they pull the data from the key passed in must match the key of the data source.
The keys that are defined in your default route for route values are controller
, action
, and id
:
url: "{controller}/{action}/{id}"
However, the key that you defined in your action method is page
:
public ActionResult Index(int page = 1)
Since there are no keys in that source named page
, no value will be passed into your action method from route values, and it will continue trying other sources such as the query string. If no value named page
is found in any of the value providers, the value will be empty and thus your default action method value will be used.
The simplest fix is to rename the key in your action method to id
:
public ActionResult Index(int id = 1)
Alternatively, create a specialized route and pass the key through as page
instead of id
:
routes.MapRoute(
name: "DefaultWithPage",
url: "Home/Index/{page}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);