Let's talk about some tough things encountered during ASP. net mvc 4 Experience (2)

Source: Internet
Author: User

In ASP. net mvc, URLRouting is also a URL routing. This is a magic function that can convert a Url into a Url that meets user habits or is more friendly to search engines. Simply post the Code:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

This is the default MVC route. now, I want to implement a URL like this: The http://www.abc.com/apple/ makes its corresponding controller Home and action google. the simple content is not much said. Simply paste the Code:

routes.MapRoute(
"Topic", // Route name
"apple", // URL with parameters
new { controller = "Home", action = "google" }// Parameter defaults
);

Now there is an Actionlink: @ Html. actionLink ("Apple", null, "apple", null, new {@ class = "abc"}), this ActionLink is in a _ Layout. cshtml, when we request this ActionLink for the first time, it will be correctly resolved to the Home controller, google Action, the address is displayed as a http://www.abc.com/apple/ after the jump, the second request for this ActionLink, the parsing is still Home controller, google Action, but the Url in the address bar will become the http://www.abc.com/Home/google if you change the null in the action to a null character will not this situation. but it is really a little painful. Why is the address bar wrong only after one jump?

Thank you for reminding me on the first floor. I reversed the action and controller above, Khan. I am very sorry. It has been modified.

In addition, the focus of my talk is to turn the Url into a http://www.abc.com/apple/. This article also means that when you set the action in Actionlink to null, (For the moment, the first digit of the Url root directory is called the second digit of controller.) If the action in the current Url is the default action of route (such as Index), the returned action is null. if not, the action in the Url will be the current action in the route. in this case, your original url is changed. if you set the action item to a null character, the Url after the jump will always be http://www.abc.com/apple/and it will become http://www.abc.com/home/google.

 

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.