0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. This is not a .Net Core project. I will take a look at the link you provided but i assume this may not be applicable as its only an MVC app?
Yes, if you are using MVC application, you should also use a httpclient library and build like the asp.net core application.
I've gone with RestSharp it seems to work with MVC (not .Net Core for this project) and so far it seems its like a wrapper for most functions that i require with less code to maintain

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.