Why are some HtmlHelper methods implemented as extension methods

I am currently creating some special helper classes similar to the ASP.NET MVC standard HtmlHelper. When I looked for implementation HtmlHelper, I noticed that most / all HTML generation methods (such as ActionLink(), BeginForm(), TextBox()etc.) are not implemented directly within HtmlHelper class, but as extension methods in separate classes (e.g., class LinkExtensions).

Besides organizing your source code better, is there any advantage to implementing methods such as extension methods instead of regular methods?

When creating my own helper classes, should I also follow this pattern?

Update : when I wrote that I wanted to create my own helper class, then I wanted to extend the existing HtmlHelper class. Instead, I created a custom base class for my views (derived from ViewPage), and I want to add an additional helper class there, similar to the Html and Url helper classes for ViewPage.

+3
source share
3 answers

The reason is that we wanted to give you the option to opt out of any of the built-in HTML helpers if you would rather write your own or use some other third-party helpers. If they were not extension methods in a special namespace, you could not ignore them.

+3

, , (4.1):

, , , .

(5.6) :

, , .

, " " , "" API .

, , (ActionLink ..), , API HtmlHelper (GenerateLink ..), .

, , , .

+3

, , , .

... .

0

Source: https://habr.com/ru/post/1728452/


All Articles