I have seen a couple related questions to this, but I cannot seem to solve my exact instance. I have a url such as http://127.0.0.1/UTS/Unit/J0000001. That is, my controller is UTS, my action is Unit and there is an optional parameter c_number.
I have my route configured as follows:
routes.MapRoute( name: "Unit", url: "{controller}/{action}/{c_number}", defaults: new { controller = "UTS", action = "Unit", c_number = "" } ); Then, my action in my controller as:
public ActionResult Unit(string c_number) { UnitViewModel uvm = new UnitViewModel(); uvm.unit = pacificRepo.Units.FirstOrDefault(g => g.c_number == c_number); uvm.geoLocation = pacificRepo.GeoLocations.FirstOrDefault(g => g.geo_location.Latitude == uvm.unit.geo_location.Latitude && g.geo_location.Longitude == uvm.unit.geo_location.Longitude); return View(uvm); } Whenever I go to the example URL I gave above, c_number comes up as null. It should be J0000001. Does anyone see something blatantly obvious that I'm missing?