Header add

 In this article we will learn how to Consume external API inside Controller in ASP.NET MVC. Suppose we have a external API then we learn here how to call it inside controller.

Let's our JSON file are available in below location

https://raw.githubusercontent.com/corespider/testfile/main/customer.json


How to call this external JSON in Controller inside ASP.NET MVC

public class HomeController : Controller
{
    public ActionResult Index()
    {
        //Fetch the JSON string from URL.
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        string json = (new WebClient()).DownloadString("https://raw.githubusercontent.com/corespider/testfile/main/customer.json");
 
        //Return the JSON string.
        return Content(json);
    }
}

When you run the application you can see the JSON like below.



Post a Comment

Previous Post Next Post