Evolution of helper in the First Anniversary of ASP. net mvc tutorial

Source: Internet
Author: User

There must be a reason for everything

Like filter, it is used to solve the problem of unified execution before or after a type of action.Code.

Helper is generated to facilitate view development.

Next we will solve several problems to see how helper evolved.

Start

Question: How do I write a super connection in view to connect to the home page?

This seems to be a good answer:

 
<A Href="/Home/Index">Homepage</A>

 

Cheng

In fact, the method above cannot be called is actually the best. Without calling any method, the problem is solved only with pure HTML.

But what if the route rule changes? Just like the original

Routes. maproute ("Default","{Controller}/{action}/{ID}",New{Controller ="Home", Action ="Index", Id =""});

Changed

 
Routes. maproute ("Default","{Controller}/ingress action=.htm",New{Controller ="Home", Action ="Index"});

 

The extension has changed. If you press the previous answer, the connection will be changed

<A Href="/Home/index.htm">Homepage</A>

 

If there are many references to this route rule in the project, such batch changes will surely be exhausting and prone to errors.

What should we do?

So we need to use urlhelper

 
<A Href="<% = URL. Action ("Index","Home") %>"> Homepage</A>
 
 
In this way, the correct address will be obtained when the route rule changes.
 
 
Transfer

The above method enables the URL to cope with changes, but there is another problem: href = "<% = %>" this method is difficult to intelligently perceive and debug in, therefore, a good debugging method is required, so the initial htmlhelper is generated.

 
<%= Html. actionlink ("Homepage", "Index", "home ")%>

 

In this way, you can easily perceive and debug the code, and the code is clean.

 

End

The above is a simple description of why helper appears. I hope my new friends who are learning ASP. NET MVC will be helpful.

 

ASP. net mvc Framework Series

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.