ASP. net mvc key tutorial version 7 back urlhelper

Source: Internet
Author: User

This section describes helper in ASP. net mvc.

Helper is actually a method group written in view to implement some flexible functions.

In fact, ASP. net mvc view is An ASPX page, which can declare the definition method. Why is there a helper?

In fact, it is nothing more than separating the interface from the logic, and Asp.net MVC does not only support aspx view (it can be expanded a lot). In the extension package, there are also Castle's nvelocity engine and Boo, as well as viewspark pages.

Therefore, defining a method in aspx affects its scalability and portability.CodeNot very nice.

1. urlhelper. Action

UrlhelperThe action method is used to generate a URL address.

The action <% = URL. Action ("Index") %> index under the current controller

The action <% = URL. Action ("Index", new {id = 1}) %> index? Id = 1

Eicecontroller index action <% = URL. Action ("Index", "eice") %>/eice/Index

The action <% = URL. Action ("Index", "eice", new {id = 1}) %>/eice/index? Id = 1

For example, I write a URL in the view. action ("Index", "home"). After running, the address/home/index is generated, if the URL routing rules in your system are always changing, This helper is a required choice.

Ii. urlhelper. encode

This is also a method for using urlhelper. For example, the <% = URL. encode ("Chinese") %> function is the same as server. urlencode.

3. urlhelper. Content

Converts a server path to an absolute path.

For example

 
<%= URL. Content ("~ /Content/site.css ")%>

The final result is

 
/Content/site.css

If you have special requirements, you can use the new 3.0 feature and Extension Method to add new features for urlhelper. The next section describes how to use helper to display HTML

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.