Skip to main content

Web Api Client Helper for .NET CORE Project

First of all, thank you for taking time to read this. I appreciate it.
I was working in a .NET core project that required to call remote Web APIs. I am sharing the code I wrote so might be useful/helpful to others. Please let me know your feedback, suggestion on how to make this package better. At the same time, I am also learning. One thing I realized is that I need to start sharing what I encounter while working on project to learn more.
And please also comment on how I can make the blog more clear especially on "How to use this package" part.

I created a NuGet package called WebApiClientHelper. Latest package can be downloaded from https://www.nuget.org/packages/WebApiClientHelper/. Or you can install from NuGet Package Manager in Visual Studio Project.

How to use WebApiClientHelper
  1.  Add an app setting called "ApiSetting" in appsettings.json file. Add a property called "BaseAddress". Value of the BaseAddress is the base URL of the Web APIs where they are being hosted.
  2. Configure in Startup.cs file:
    AddOptions - it uses IOption to load the appsettings variables from appsettings.json file.
    Configure the "ApiSetting" that was added in appsettings.json file.
    Add the ApiHttpClient in Dependency Inject (DI) container so it is available in your controller at run time.

  3. Now you can inject the ApiHttpClient in your controller:
    public class YourController: Controller
    {
             private readonly IApiHttpClient _apiHttpClient

            public YourController(IApiHttpClient apiHttpClient)
            {
                    _apiHttpClient = apiHttpClient;
            }

            public ActionResult GetProducts()
            {
                    var response = _apiHttpClient.GetAsync<Product>("api/GetProductsList");
                    if (response.Result.IsSuccessful)
                    {
                           var productList = response.Result.DataCollection;
                     }
                    .............
            }

            public ActionResult PostProduct(ProductViewModel model)
            {
                   Product prd = <get product object from ProductViewModel>;
                    .............
                    var response = _apiHttpClient.PostAsync<Product>(prd, "api/postproduct");

                    if (response.Result.IsSuccessful)
                    {
                           ........................
                     }
            }
    }

Comments

Popular posts from this blog

RedirectToAction method in AspNetCore (.NET CORE) MVC not working

I needed to redirect to different action method after some work in another action method. I did use RedirectToAction but .NET Core MVC application was only displaying blank. Scenario was: public ActionResult Method1() {        ..........        ..........        var temp = (List<object>)TempData["KeyName"]; } public ActionResult Method2() {      ..............      List<object> obj = {my list of object};      TempData["keyName"] = obj;      RedirectToAction("Method1") } However, application displayed blank page and in URL it was like this "http://localhost/{controller name}/Method2 So, what was the problem? Problem was not in RedirectToAction method. Problem was in the way I assigned data to TempData. To fix the problem, I had to serialize the data before assigning the data to TempData. Solution for the above scenario: public ActionResult Method1() {        ..........        ..........        var temp = JsonConve