MVC route -- & gt; Link, mvc route -- Link

Source: Internet
Author: User
Tags actionlink

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.