0

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?

1
  • Is that the only route you have defined? Commented Mar 10, 2014 at 23:19

1 Answer 1

1

Since you define it in your Route you don't need to add parameter for your c_number.You can get your value from RouteData dictionary.That c_number parameter has value only if you pass it as QueryString like http://someurl.com/UTS/Unit?c_number="J0000001"

public ActionResult Unit() { var cNumber = RouteData.Values["c_number"].ToString(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! This works. I am able to extract it as long as it is passed like a QueryString. Is it possible to define the route like I have above? So that you could access it via http://127.0.0.1/UTS/Unit/J0000001. This seems to work if the parameter is named "id" -- is this a special case variable?
@gnychis RouteData.Values["c_number"] should work with your current RouteConfig.Your definition is correct.just delete the parameter and get the value from RouteData
ohhh I see what you mean now. Thanks for this!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.