In this article I will explain with an example, how to call (consume) an external REST API inside Handler Method in ASP.Net Razor Pages.
The external REST API will be called using WebClient class in ASP.Net Core Razor Pages.
ASPSnippets Test API
The following API will be used in this article.
The API returns the following JSON.
      "Name":"John Hammond",
      "Country":"United States"
      "Name":"Mudassar Khan",
      "Name":"Suzanne Mathews",
      "Name":"Robert Schidner",
You will need to import the following namespace.
using System.Net;
Razor PageModel (Code-Behind)
The PageModel consists of the following Handler method.
Handler method for handling GET operation
Inside this Handler method, first the JSON string is downloaded from the API using DownloadString method of the WebClient class.
Note: SecurityProtocol needs to be set to TLS 1.2 (3072) in order to call an API.
Finally, the JSON string is set in the Message property.
public class IndexModel : PageModel
    public string Message { get; set; }
    public void OnGet()
        //Fetch the JSON string from URL.
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        string json = (new WebClient()).DownloadString("");
        this.Message = json;
Razor Page (HTML)
Inside the HTML of Razor Page, the JSON string is displayed using Message property.
@model WebClient_Razor_Core.Pages.IndexModel
    Layout = null;
<!DOCTYPE html>
    <meta name="viewport" content="width=device-width" />
ASP.Net Core Razor Pages: Call (Consume) external API inside Handler Method