How to consume an ASP.NET Core Web API using RestSharp

0
140


REST is an acronym for Representational State Transfer, an architectural style that has become extremely popular over the past few years. A RESTful API is one that it is built in accordance with . RESTful APIs usually return plain text, JSON, or XML as a response.

is an open source HTTP client library that makes it easy to consume RESTful services. RestSharp provides a developer friendly interface to work with RESTful services while abstracting the internal intricacies of working with HTTP requests. RestSharp supports both synchronous and asynchronous requests.

This article presents a discussion of how we can work with RestSharp to consume services built using ASP.NET Core.

To work with the code examples in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can . 

Create an ASP.NET Core API project

First off, let’s create an ASP.NET Core project in Visual Studio. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.Net Core project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Click Create.
  7. In the “Create New ASP.Net Core Web Application” window, select .NET Core as the runtime and ASP.NET Core 2.2 (or later) from the drop-down list at the top. I’ll be using ASP.NET Core 3.0 here.
  8. Select “API” as the project template to create a new ASP.NET Core API application. 
  9. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
  10. Ensure that Authentication is set as “No Authentication” as we won’t be using authentication either.
  11. Click Create. 

Following these steps will create a new ASP.NET Core API project in Visual Studio. Next, select the Controllers solution folder in the Solution Explorer window, click “Add -> Controller…,” and select “API Controller with Read/Write Actions.” Name this new controller DefaultController.

or . Note that I’ve hardcoded the ID in the HttpPost method just for simplicity. You should implement it in your own way to generate an unique key.

So far so good. In the sections that follow we’ll learn how to work with RestSharp to consume the API we’ve built.

Create the client to consume the API

We’ll be using a console application as the client to consume the API we built earlier. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new .NET Core Console Application project in Visual Studio.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “Console App (.NET Core)” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window shown next, specify the name and location for the new project.
  6. Click Create.

That’s all we have to do to create a new .NET Core Console Application project.