How to Consume ASP.Net WEB API From ASP.Net MVC4 Using RestSharp

Contents

  1. Abstract
  2. Introduction
  3. Background
  4. Pre-requisites
  5. Why RestSharp
  6. What we consume
  7. Create the Web Project
  8. Add RestSharp Support to Project
  9. Add Rest Client
  10. Create Model Class
  11. Describing Model Class
  12. Why All the Checks Are On the Client Side
  13. Create Controller
  14. Describing Controller
  15. Create View
  16. Executing the Project
  17. Closing Notes

Abstract

Throughout the article we will learn how to call APIs hosted on another server (as a REST services only) from ASP.NET MVC4 (as a client) using RESTSHARP.

Introduction

In most scenarios, we develop an application of ASP.NET MVC4 using the ASP.NET WEB API. But a RESTFul service like the ASP.NET WEB API is meant to be used by various clients. In this article we will see how to consume a simple ASP.NET WEB API using ASP.NET MVC4 (as a client) with RestSharp (simple Rest and HTTP client for .Net).

Background

Before going through this article, I recommend your read my previous article where I defined how to create an ASP.NET WEB API (CRUD Operations using Fluent NHibernate).

Prerequisites

To implement and play with the source code one should have:

  • Visual Studio 2013 Express or later
  • ASP.NET MVC4 or later
  • ASP.NET WEB API2 or later
  • Basic knowledge of RestFul Services
  • Basic knowledge of ASP.NET WEB API
  • Basic knowledge of ASP.NET MVC4 or later

Why RestSharp

Since we will use RestSharp to call our ASP.NET WEB API URIs, first let's discuss "RestSharp". This is nothing but a Simple REST and HTTP API Client for .NET. It provides us a simple way to just initialize a RestClient, pass a Request, define a Method (GET, POST, PUT, DELETE) and get a response.

The followings shows a commonly used snippet:

  1. //Create a restclient  
  2. RestClient restClient = RestClient("url");  
  3. //Create request with GET  
  4. var request = new RestRequest("api/serverdata", Method.GET); 

Do not forget to provide RequestFormat and RestSharp to automatically serialize/deserialize complex objects like Models like ServerDataModel in our demo project.

  1. //Do not forget while using complex types  
  2. var request = new RestRequest("api/serverdata", Method.GET)  
  3.  {RequestFormat = DataFormat.Json}; 

The following will get a formatted result for complex types:

var response = restClient.Execute<List<ServerDataModel>>(request);

There are many more features, we are not covering in this article. Please refer to RestSharp Wiki .

What we consume

As said earlier in this article, we will consume existing ASP.NET Web API URIs (hosted on a server). The following is the table showing all the URIs:



Refer to the demo app: Demo ASP.NET WEB API.

Create the Web Project

Let's start the creation of our new ASP.NET project (client project) using the following:

Start Visual Studio and select "File" -> "New" -> "Project..." (or enter Ctrl + Shift + N).



In the Templates dialog, select Installed Templates and then expand the Visual C# node. Under Visual C#, select Web. In the list of project templates, select ASP.NET MVC4 Web Application, name the project and click "OK".

I named my project "consumewebapi", you can choose whatever you want.

From the Project Template, select Internet Application and Razor from the View Engine dropdown list and click "OK".



You can also check the "Create unit test project" checkbox if you are doing Test driven development and I highly recommend doing that. In this article, we are not covering that part.

Now, after the preceding step(s), we have a default ASP.NET MVC4 project template with us, later on we will add to it.



Add RestSharp Support to Project

We will use RestSharp to consume our ASP.NET WEB API, use this procedure:

Go to Tools -> NuGet Package Manager -> Package Manager Console.

From the Package Manager Console, run the following command:

Install-Package RestSharp




Add Rest Client

1. From Solution Explorer, create a new folder and name it Helper. This folder will contain all our helper classes required for the RestSharp client (these classes will play an interface between our client and services).

2. Add an interface and name it IServerDataRestClient.

The complete code of IServerDataRestClient looks as in:

  1. public interface IServerDataRestClient  
  2. {  
  3.     void Add(ServerDataModel serverDataModel);  
  4.     void Delete(int id);  
  5.     IEnumerable<serverdatamodel> GetAll();  
  6.     ServerDataModel GetById(int id);  
  7.     ServerDataModel GetByIP(int ip);  
  8.     ServerDataModel GetByType(int type);  
  9.     void Update(ServerDataModel serverDataModel);  

3. Add a new class under Helper folder and name it ServerDataRestClient and implement the interface IServerDataRestClient:

  1. public class ServerDataRestClient : IServerDataRestClient  
  2. {  
  3.  //implementation  

We need to create a RestClient, create a variable private readonly RestClient _client in the class and initialize it in the constructor.

  1. //initializing RestClient  
  2. public ServerDataRestClient()  
  3. {  
  4.  _client = new RestClient(_url);  

In the preceding, while we are initializing the RestClient object, we are providing a BaseUrl, nothing but the complete name of the URL like http://gaurav-arora.com

We can also do this:

  1. _client = new RestClient {BaseUrl = _url}; 

We are fetching the base URL from our config files, why it is in config files. It is recommended for large projects where we have multiple environments like Dev, Staging, QA, Prod and so on.

  1. //getting base url from config  
  2. private readonly string _url = ConfigurationManager.AppSettings["webapibaseurl"]; 

Now, we require a request that contains nothing but a resource and a method.



By providing {RequestFormat = DataFormat.Json}; we are telling RestClient to provide us an output in the form of JSON. This is required when working with complex classes, like here we are using the ServerDataModel class.

In DEBUG mode, check _client in Quick watch (Ctrl + D,Q). You will find everything we set when we created our RestClient object.



Our client is ready to convey our request and get the response.

  1. var response = _client.Execute<List<ServerDataModel>>(request); 

The preceding will provide us the output in a list of our ServerDataModel class.

Open Quick watch for the response, you will get the following two nodes:

[RestSharp.RestResponse<System.Collections.Generic.List<ConsumeWebAPI.Models.ServerDataModel>>] {RestSharp.RestResponse<System.Collections.Generic.List<ConsumeWebAPI.Models.ServerDataModel>>} RestSharp.RestResponse<System.Collections.Generic.List<ConsumeWebAPI.Models.ServerDataModel>> and Data.



The first node contains our actual content in JSON format with other information. For example, ContentLength, ContentType, Cookies, ErrorMessage, Header, Request, ResponseStatus, StatusCode and so on.

The Data node contains our formatted data as we requested and required. In our case, it is a list of type ServerDataModel. We will use this model and our view will show later.

We can also request a specific resource like if we need record by id, then we can do:

  1. var request = new RestRequest("api/serverdata/{id}", Method.GET) {RequestFormat = DataFormat.Json};  
  2.   
  3. request.AddParameter("id", id, ParameterType.UrlSegment); 

The following will provide us output of type ServerDataModel:

var response = _client.Execute<serverdatamodel>(request);

The following is our complete ServerDataRestClient helper class:

  1. public class ServerDataRestClient : IServerDataRestClient  
  2.     {  
  3.         private readonly RestClient _client;  
  4.         private readonly string _url = ConfigurationManager.AppSettings["webapibaseurl"];  
  5.   
  6.         public ServerDataRestClient()  
  7.         {  
  8.             _client = new RestClient {BaseUrl = _url};  
  9.         }  
  10.   
  11.         public IEnumerable<serverdatamodel> GetAll()  
  12.         {  
  13.             var request = new RestRequest("api/serverdata", Method.GET) {RequestFormat = DataFormat.Json};  
  14.   
  15.             var response = _client.Execute<list<serverdatamodel>>(request);  
  16.   
  17.             if (response.Data == null)  
  18.                 throw new Exception(response.ErrorMessage);  
  19.   
  20.             return response.Data;  
  21.         }  
  22.   
  23.         public ServerDataModel GetById(int id)  
  24.         {  
  25.             var request = new RestRequest("api/serverdata/{id}", Method.GET) {RequestFormat = DataFormat.Json};  
  26.   
  27.             request.AddParameter("id", id, ParameterType.UrlSegment);  
  28.   
  29.             var response = _client.Execute<serverdatamodel>(request);  
  30.   
  31.             if (response.Data == null)  
  32.                 throw new Exception(response.ErrorMessage);  
  33.   
  34.             return response.Data;  
  35.         }  
  36.   
  37.         public ServerDataModel GetByType(int type)  
  38.         {  
  39.             var request = new RestRequest("api/serverdata/type/{datatype}", Method.GET)  
  40.             {  
  41.                 RequestFormat = DataFormat.Json  
  42.             };  
  43.   
  44.             request.AddParameter("datatype", type, ParameterType.UrlSegment);  
  45.   
  46.             var response = _client.Execute<serverdatamodel>(request);  
  47.   
  48.             return response.Data;  
  49.         }  
  50.   
  51.         public ServerDataModel GetByIP(int ip)  
  52.         {  
  53.             var request = new RestRequest("api/serverdata/ip/{ip}", Method.GET) {RequestFormat = DataFormat.Json};  
  54.             request.AddParameter("ip", ip, ParameterType.UrlSegment);  
  55.   
  56.             var response = _client.Execute<serverdatamodel>(request);  
  57.   
  58.             return response.Data;  
  59.         }  
  60.   
  61.         public void Add(ServerDataModel serverData)  
  62.         {  
  63.             var request = new RestRequest("api/serverdata", Method.POST) {RequestFormat = DataFormat.Json};  
  64.             request.AddBody(serverData);  
  65.   
  66.             var response = _client.Execute<serverdatamodel>(request);  
  67.   
  68.             if (response.StatusCode != HttpStatusCode.Created)  
  69.                 throw new Exception(response.ErrorMessage);  
  70.         }  
  71.   
  72.         public void Update(ServerDataModel serverData)  
  73.         {  
  74.             var request = new RestRequest("api/serverdata/{id}", Method.PUT) {RequestFormat = DataFormat.Json};  
  75.             request.AddParameter("id", serverData.Id, ParameterType.UrlSegment);  
  76.             request.AddBody(serverData);  
  77.   
  78.             var response = _client.Execute<serverdatamodel>(request);  
  79.   
  80.             if (response.StatusCode == HttpStatusCode.NotFound)  
  81.                 throw new Exception(response.ErrorMessage);  
  82.         }  
  83.   
  84.         public void Delete(int id)  
  85.         {  
  86.             var request = new RestRequest("api/serverdata/{id}", Method.DELETE);  
  87.             request.AddParameter("id", id, ParameterType.UrlSegment);  
  88.   
  89.             var response = _client.Execute<serverdatamodel>(request);  
  90.   
  91.             if (response.StatusCode == HttpStatusCode.NotFound)  
  92.                 throw new Exception(response.ErrorMessage);  
  93.         }  
  94.     } 

Create Model Class

Under Solution Explorer, right-click on the Models folder and Add -> New Item (or hit Ctrl + Shift + A). Choose the class and name it ServerDataModel and click "Add". It will add an empty Model class.

Here is our complete model class:

  1. public class ServerDataModel  
  2.     {  
  3.         public int Id { getset; }  
  4.         [Required]  
  5.         [Display(Name = "Initial date")]  
  6.         [DataType(DataType.Date)]  
  7.         [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]  
  8.         public DateTime InitialDate { getset; }  
  9.         [Required]  
  10.         [Display(Name = "End date")]  
  11.         [DataType(DataType.Date)]  
  12.         [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]  
  13.         public DateTime EndDate { getset; }  
  14.   
  15.         [Required]  
  16.         [Display(Name = "Order number")]  
  17.         public int OrderNumber { getset; }  
  18.         [Required]  
  19.         [Display(Name = "Is dirty")]  
  20.         public bool IsDirty { getset; }  
  21.         [Required, StringLength(15)]  
  22.         [Display(Name = "Data Server IP")]  
  23.         [RegularExpression(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)  
  24.         {3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$",  
  25.         ErrorMessage = "Data Server IP should be in the form of 255.255.255")]  
  26.         public string IP { getset; }  
  27.   
  28.         [Required]  
  29.         [Display(Name = "Record data type")]  
  30.         [RegularExpression(@"^([1-2])$",   
  31.         ErrorMessage = "Record Data Type should be 1 or 2")]  
  32.         public int Type { getset; }  
  33.   
  34.         [Display(Name = "Record identifier")]  
  35.         [RegularExpression(@"^([0-9])$",   
  36.         ErrorMessage = "Record identifier should be between 0 to 9")]  
  37.         public int RecordIdentifier { getset; }  
  38.     } 

Describing Model Class

Here are some points we should notice in our model class:

1. Initial Date and End Date should be of Date type and formatted as MM/dd/yyyy:

  1. [DataType(DataType.Date)]  
  2.     [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]  
  3.     public DateTime InitialDate { getset; } 

2. Here, we are using Data annotations.

3. We set ApplyFormatInEditMode = true so that while in mode, the user shouldn't forget to supply the required format.

4. Our date fields should contain a Calendar control on the browsers that obeys HTML5 rules.



5. IP should be formatted in the actual IP format (to avoid entering unwanted strings).

  1. [Display(Name = "Data Server IP")]  
  2. [RegularExpression(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.)  
  3. {3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$",  
  4. ErrorMessage = "Data Server IP should be in the form of 255.255.255")]  
  5. public string IP { getset; } 

6. Here, we used RegularExpression, the app will throw an error if it doesn't match.



7. The record data type should be 1 or 2.

  1. [Required]  
  2. [Display(Name = "Record data type")]  
  3. [RegularExpression(@"^([1-2])$", ErrorMessage = "Record Data Type should be 1 or 2")]  
  4. public int Type { getset; } 

8. If the expression does not match, it will throw an error message.



9. Record identifier should be in the range 0-9.

10. If the expression does not match, it will throw an error message as in the following:



Why All Checks Are On the Client Side?

In this article, we are implementing the ASP.NET WEB API in our client project that is in ASP.NET MVC4. Here we want to ensure that each and every request should be verified and error-free. There are many debates on this approach, whether to implement it client-side or at services-side. In my view, services is the centralized process and it would be calling from multiple clients, so, it's the responsibility of each and every client of an incoming request to be verified and error-free (here incoming request does not contain any security logic, here I meant to say only about normal validation data checks).

Create Controller

1. Under Solution Explorer, right-click on the Controllers folder and click Controller.



2. From the Add controller dialog, enter the Controller name ServerDataController and select MVC controller with empty read/write actions from the Template dropdown, click on "Add".



Describing Controller

Our ServerDataController will consume an ASP.NET WEB API using RestSharp (we'll use our helper class, created above).

  1. static readonly IServerDataRestClient RestClient = new ServerDataRestClient();  
  2.   
  3. Above, we simply initialized our ServerDataRestClient  
  4. private IServerDataRestClient _restClient;  
  5.   
  6. public ServerDataController(IServerDataRestClient  restClient)  
  7. {  
  8.    _restClient = restClient;  

The preceding is another flavor where we need to use Inversion Of Control (IOC).

  1. public ActionResult Index()  
  2. {  
  3.    return View(RestClient.GetAll());  

In the preceding, our Index ActionResult method will get all ServerData records from services and render our view. We need to add views at this point.

Create View

At this point, we need a UI to show output or provide an interface to our user to interact with the application. So, we need to add a view.

The direct/rough way to add a view is, from Solution Explorer, add a new folder ServerData and right-click on it, then click on Add View.



From the Add View dialog, name your view like Index. Select Razor as the ViewEngine, create a strongly typed view and select ServerDataModel as a Model class. I use a Scaffold Template of List (you can ignore that if do not want to use it), use your master layout and click on "Add".



It will add a new view to the project.

  1. @model IEnumerable<consumewebapi.models.serverdatamodel>  
  2.   
  3. @{ ViewBag.Title = "Manipulate Server Data";  
  4. Layout = "~/Views/Shared/_Layout.cshtml";  
  5. }</consumewebapi.models.serverdatamodel> 

We did a little bit of manipulation to get the output:

  1. @string.Format(item.IsDirty ? "Yes" : "No"

It will give us Yes or No showing whether or not our Record is dirty.

The following same procedure is to add other views, here is the structure:



Executing the Project

Finally, we are done with all of our customizations and changes. It's now time to run our project. Just click run or hit F5. Our Index page will be rendered as:



Closing Notes

  • Ah! ASP.NET MVC4 does not provide direct implementation to use RestSharp. There is a good way to add RestSharp supports to our project and then we can use this awesome framework.
  • I have created a demo app here, you can use this and adapt the code in part or as a whole. I will be glad to hear from you if you find any issues or other things you need to handle.