In this article I will explain with an example, how to download JSON file from URL in ASP.Net MVC.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
The JSON file will be downloaded from the URL using WebClient class and then will be saved in a Folder (Directory).
 
 

JSON File URL

The following JSON file will be used in this article.
[
 {
    "CustomerId": 1,
    "Name""John Hammond",
    "Country""United States"
 },
 {
    "CustomerId": 2,
    "Name""Mudassar Khan",
    "Country""India"
 },
 {
    "CustomerId": 3,
    "Name""Suzanne Mathews",
    "Country""France"
 },
 {
    "CustomerId": 4,
    "Name""Robert Schidner",
    "Country""Russia"
 }
]
 
 

Namespaces

You will need to import the following namespace.
using System.Net;
 
 

Controller

The Controller consists of following Action method.

Action method for handling GET operation

Inside this Action method, the JSON file is downloaded from the URL using DownloadFile method of the WebClient class.

DownloadFile method

It accepts the following two parameters:
address – The URL of the file to be downloaded.
fileName – Path of the Folder (Directory) where the file will be downloaded.
public class HomeController : Controller
{
    public ActionResult Index()
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        WebClient webClient = new WebClient();
        webClient.DownloadFile("https://raw.githubusercontent.com/aspsnippets/test/master/Customers.json"@"D:\Files\Customers.json");
        return View();
    }
}
 
 

View

HTML Markup

There is no coding part in View page. Hence, it is skipped.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
</body>
</html>
 
 

Screenshot

The downloaded JSON file

ASP.Net MVC: Download JSON File form URL
 
 

Downloads