Home > Back-end >  How to call multiple WebAPI methods in a separate MVC project?
How to call multiple WebAPI methods in a separate MVC project?

Time:08-12

New with MVC and WebAPI and going through a few tutorials.

I have created a WebAPI project. This project calls another Class Library (Unit of Work) to return its classes/methods such as a list of products a single product etc etc. All is working in Postman.

I create a new MVC project and would like to call this WebAPI project to retrieve the data and place it on my page.

I decided to use WebAPI so it could be used on other platforms i.e phones etc. My first attempt i wrote some code using the HttpClient in the MVC project but i am finding that i'm close to repeating multiple HttpClient calls to retrieve methods such as GetProducts, GetProduct, AddProduct, DeleteProduct.

Is there a better way to call these methods without using HttpClient in code behind? Or at least a structured approach so i dont always have to rebuild the project to call a different method in case i selected the wrong one against a specific page?

CodePudding user response:

If you want to call the web api inside the asp.net core MVC application, you should use the Httpclient or other package like RestSharp which build on the httpclient.

Normally, in the project we will write a respostory and has the codes which could send the http request and serilize the result which sent from the web api.

I suggest you could also try this way. More details, you could refer to this article.

  • Related