I’m looking at the following jQuery call:
$.get('/Home/GetResult', null, function (data) {
alert(data);
});
I see this being called the ‘Hard Coded’ way of doing this compared to:
$.get('@Url.Action("GetResult", "Home")', function (data) {
alert(data);
});
which I see referred to as the non hard coded way of doing this.
Could someone please explain to me what this is all about? Seems to me both ways are equally hardcoded ways of calling this because you have the explicit names of the Controller and Method in both.
Does anyone know why one is called hardcoded and the other isn’t. Is one better?
If we hard code a URL then we lose the ability to later change our routing scheme. So always use URL Helper methods. If you use URL helper, the Helper method will take care of the change, You do not need to go and make the change in 100 places. Also you do not need to worry about how many ../ i have to add as the prefix when the same method is being called from a Home page and an inner page
Checkout these answers by Darin
https://stackoverflow.com/a/7492024/40521
https://stackoverflow.com/a/6454041/40521