This is an example repo corresponding to multiple lessons within the LearnHowToProgram.com walkthrough on making API calls in C# in both a console app and an ASP.NET Core MVC app in Section 5: Authentication with Identity. The first lesson in the series is Making an API Call with RestSharp.
There are multiple branches in this repo that are described more below.
Finally, note that these projects were scaffolded with the dotnet new
tool.
Install the tools that are introduced in this series of lessons on LearnHowToProgram.com.
This project uses the New York Times' Top Stories API to practice making API calls in C#. In order to use the Top Stories API, you'll need to create a free New York Times developer account. Follow the Get Started steps to create an application and get your own API key.
The following setup instructions are for the branches 1_api_call_in_console_app
and 2_deserializing_responses_in_console_app
only.
- Clone this repo.
- Open the terminal and navigate to this project's production directory. called "ConsoleApiCall".
- Within the production directory "ConsoleApiCall", create a new file called
EnvironmentVariables.cs
. - Within
EnvironmentVariables.cs
, put in the following code, replacing the[YOUR-API-KEY-HERE]
with your own New York Times API key.
namespace ConsoleApiCall.Keys
{
public static class EnvironmentVariables
{
public static string ApiKey = "[YOUR-API-KEY-HERE]";
}
}
- Within the production directory "ConsoleApiCall", run
dotnet run
in the command line to start the project in development mode with a watcher.
The following setup instructions are for the branch 3_api_call_in_mvc_app
only.
- Clone this repo.
- Open the terminal and navigate to this project's production directory. called "MvcApiCall".
- Within the production directory "MvcApiCall", create a new file called
appsettings.json
. - Within
appsettings.json
, put in the following code, replacing the[YOUR-KEY-HERE]
with your own New York Times API key.
{
"NYT": "[YOUR-KEY-HERE]"
}
- Within the production directory "MvcApiCall", run
dotnet watch run
in the command line to start the project in development mode with a watcher. - Open the browser to https://localhost:5001. If you cannot access localhost:5001 it is likely because you have not configured a .NET developer security certificate for HTTPS. To learn about this, review this lesson: Redirecting to HTTPS and Issuing a Security Certificate.
1_api_call_in_console_app: This branch includes the code we added after working through the following lesson:
2_deserializing_responses_in_console_app: This branch includes the code we added after working through the following lesson:
3_api_call_in_mvc_app: This branch includes the code we added after working through the following lesson: