My issue is that I made a Map Route in MVC which takes three parameters. When I supply all three or just two, the parameters are passed from the URL to my controller. However, when I only supply the first parameter, it is not passed and returns null. Not sure what causes this behavior.
Route:
routes.MapRoute(
name: "Details", // Route name
url: "{controller}/{action}/{param1}/{param2}/{param3}", // URL with parameters
defaults: new { controller = "Details", action = "Index", param1 = UrlParameter.Optional, param2 = UrlParameter.Optional, param3 = UrlParameter.Optional } // Parameter defaults
);
Controller:
public ActionResult Map(string param1, string param2, string param3)
{
StoreMap makeMap = new StoreMap();
var storemap = makeMap.makeStoreMap(param1, param2, param3);
var model = storemap;
return View(model);
}
string param1 returns null when I navigate to:
/StoreMap/Map/PARAM1NAME
but it doesn’t return null when I navigate to:
/StoreMap/Map/PARAM1NAME/PARAM2NAME
Most likely the default route is interfering. I believe the default route defined in the project template looks like this:
Your URL with only one parameter matches this pattern, but since you don’t have an
idparameter in your method signature, the value doesn’t get populated into any of your parameters.You could try altering your “Details” route to hard-code the controller to be “Details”, as shown below, and move it so that it comes before the default route:
Alternatively, try renaming the first parameter in your route and your method signature to
id.