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