MVC route --> link, mvc route -- Link
The link address, which can be dynamically generated according to routing rules, does not need to be written to death. When the document structure changes, you can still find the correct path
1. url. Action (only generate URL)
There are many overload methods. You can select the method as needed. When a URL is generated, it is generated according to the routing rules. However, the method is also addressing according to the routing rules during the call, so OK
<A href = "@ Url. Action (" GetStuDetail "," Student ", new {id = 1001, gender = 1})"> Student details </a>
// Route rule routes. mapRoute (name: "Default", url: "{controller}/{action}/{id}", defaults: new {controller = "Home", action = "Index ", id = UrlParameter. optional}); // matching result <a href = "/Student/GetStuDetail/1001? Gender = 1 "> Student details </a>
2. Html. ActionLink (generate the entire Link tag)
// Generate tag @ Html on the View page. actionLink ("new Student", "AddNewStu", "Student", new {gender = 1, age = 10, name = "Li Lei "}, // Url parameter new {@ class = "AddStuLink", title = "new student"}) // Html tag attributes // route rule routes. mapRoute (name: "Test1", url: "{action}/{controller}", defaults: new {controller = "Home", action = "Index "}) // generated link <a title = "add Student" class = "AddStuLink" href = "/AddNewStu/Student? Gender = 1 & age = 10 & name = % E6 % 9D % 8E % E7 % A3 % 8A "> Add students </a>
Tags generate URLs Based on routing rules. Therefore, actions are in the front. After the controller is in the front, there must be a Controller and action in the routing, but the location is not required, which can be blocked.