Route data RouteData. Values [key],
RoutData. values [Key]Only data in the route definition can be obtained. The url parameter data is not included.
Https:// I .cnblogs.com/EditPosts.aspx? Opt = 1
Route: domain name? Between -->EditPosts. aspx
URL parameter :? After -->Opt = 1
// Add link on the View page <a href = '@ Url. action ("StudentDetail", "Student", new {id = 1001, age = 20, gender = 1}) '> View Student details </a> // route table routes. mapRoute (name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index ", id = 10 });
// The mapped address is as follows:
<A href = "/Student/StudentDetail/1001? Age = 20 & amp; gender = 1 "> View student details </a>
// The public ActionResult StudentDetail (int age, int gender) method in the Controller {// The data ViewBag in RouteData is used. id = RouteData. values ["id"]; // The method parameter ing is used. age = age; ViewBag. gender = gender; return View ();} // display in the StudentDetail View <div> @ * output data obtained from the Controller in the View * @ ViewBag. id <br/> @ ViewBag. gender <br/> @ * output directly in the view * @ ViewContext. routeData. values ["controller"] <br/> @ * traverse the view * {foreach (KeyValuePair <string, object> data in @ ViewContext. routeData. values) {var info = data. key + ":" + data. value; @ info @ * display to page * @ <br/>}</div>
1001 //
1
Student
// Because the route has only the following three parameters, the age and gender are not obtained through traversal.
Controller: Student
Action: StudentDetail
Id: 1001
After modifying the routing rule
// Link configuration <a href = '@ Url. action ("StudentDetail", "Student", new {id = 1001, age = 20, gender = 1}) '> View Student details </a> // route rule routes. mapRoute (name: "Test1", url: "{controller}/{action}/{id}/{age}/{gender}", defaults: new {controller = "Home", action = "Index", id = 10 }); // generate a tag <a href = "/Student/StudentDetail/1001/20/1"> View Student details </a> // traverse RouteData. the result of values is as follows: controller: Student action: StudentDetail id: 1001 age: 20 gender: 1