What are the benefits of using HTML MVC helpers like ActionLink, BeginForm, TextBox, etc. Instead of custom HTML tags?

In the answer of SO to another question, the user stated that it allows you to avoid hard-coding route values ​​in the html link tag, but this is not entirely true as you have to enter the controller, action, scope, etc., so that you still hard-coded route values.

Like this:

@Html.ActionLink(linkText: "MyLink", actionName: "MyAction", controllerName: "MyController", new { id = @myId }, new { area = "SomeArea"}) 

better than that:

 <a href='/SomeArea/MyController/MyAction/myId'>MyLink</a> 
+6
source share
2 answers

Your observation is true only if (a) you strictly use the default routing format and (b) if your application will always be installed in the root directory of the site. If you don’t do the first (say, create a short route / help that goes to the main controller and the Help action, and then change it by introducing a control controller with a lot of actions, then you will need to update all your hard-coded anchor labels. A better alternative is to use the RouteLink helper with the name of the route and, optionally, other parameters.

As for the latter, I usually use one server for most of my intermediate deployments, and the application does NOT sit on the root of the site, but rather in a subdirectory. Production deployment is mixed, but many applications are installed at the root of the site. Using helpers allows me to ignore the difference during development, since the helper correctly structures the URL relative to the current site in all cases. It is so useful that I even use it for scripts, css files, images, etc. Through UrlHelper to make sure that any paths specified for them are not broken between stages and production.

+3
source

It seems that there is little benefit from using the assistant, if you make one change, add a tilde so that the router automatically resolves the address to the right place.

 <a href='~/SomeArea/MyController/MyAction/myId'>MyLink</a> 
0
source

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


All Articles