简体   繁体   中英

ASP.NET Core MVC calling an API

This might for some people be a pretty obvious question but i am pretty new to programming and Web Development.

If i need to call an API from an ASP.NET Core MVC application what is the best practice, to do it in C# under the Controller or during it with JavaScript, as i understand both options are available. I am assuming it is best practice do to it in the controller with C# code as the code is (from what i know) not vissible in developer tools as it is with JavaScript.

Are there any reasons to choose the JS code instead of doing it all in C# code?

/Thomas

As far as I know, if you put all codes inside controller to call the web api. it will cause problem. All the page will be refreshed when the client want to post something to backend API.

But if you use js to achieve the same thing, it will not cause this issue. Since by using ajax, it will not make the whole page refresh. It will just post the request to web api and do something after the web api returned the response. This will make your page load speeder and increase the customer experience and reduce the server's load time and reduce server's workload.

Depend on what you are supposed to do.

  1. If you would call an third parties, other services or other components. You should implement it in your data layer.
  2. If you just call an your own API (same solution), you should call in javascript code.

Thank you for the answers it helped

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM