Header add


In this article we will learn how to consume a restful API in .NET Core and get the bank details using IFSC Code. In this article we will cover below parts
>> How and where to consume Restful API ?
>> How to use Restful API to get the bank details using IFSC ?

How and where to consume Restful API 

To use https://ifsc.datayuge.com/ we call the restful API in out .Net Core application. First need to register through this portal https://ifsc.datayuge.com/register
After registration, login with the credentials then it navigate to below page and you need to copy the API authentication header and the API URL (as highlighted below)

How to use Restful API to get the bank details using IFSC
Let's create .NET Core application and consume the API.
Step-1
Open VS 2019 ➦Choose .NET Core Web Application
Step-2
Create a Model class to hold desire information of bank.
     public class BankDetails  
       {  
         public string bank { get; set; }  
         public string IFSC { get; set; }  
         public string MICR { get; set; }  
         public string branch { get; set; }  
         public string Address { get; set; }  
         public string Contact { get; set; }  
         public string City { get; set; }  
         public string District { get; set; }  
         public string State { get; set; }  
       }  
Step-3
Create a Interface Repository to hold the information list.
    public interface IBankRepository  
       {  
         BankDetails GetBankDetails(string IFSC);  
       }  
Implement the interface into the repository class and call the restful API as below.
    public class BankRepository : IBankRepository  
       {  
         public BankDetails GetBankDetails(string IFSC)  
         {  
           try  
           {  
             using (var client = new WebClient())  
             {  
               BankDetails _bank = new BankDetails();  
               client.Headers.Add("DY-X-Authorization: 8fe69b30f07a07692796fbd751d625a7d0920d1c");  
               var result = client.DownloadString("https://ifsc.datayuge.com/api/v1/" + IFSC.Trim());  
               var BankResult = JsonConvert.DeserializeObject<BankDetails>(result);  
               _bank.bank = BankResult.bank;  
               _bank.IFSC = BankResult.IFSC;  
               _bank.MICR = BankResult.MICR;  
               _bank.branch = BankResult.branch;  
               _bank.Address = BankResult.Address;  
               _bank.Contact = BankResult.Contact;  
               _bank.City = BankResult.City;  
               _bank.District = BankResult.District;  
               _bank.State = BankResult.State;  
               return _bank;  
             }  
           }  
           catch (Exception ex)  
           {  
             throw ex;  
           }  
         }  
       }  
Code Explanation
  • You can see when will execute GetBankDetails() it hold the IFSC Code and sent into Restful API then it process and gives the output.
client.Headers.Add("DY-X-Authorization: 8fe69b30f07a07692796fbd751d625a7d0920d1c");
var result = client.DownloadString("https://ifsc.datayuge.com/api/v1/" + IFSC.Trim());
Step-4
As we use dependency injection then we need to add into ConfigureServices inside startup.cs
    public void ConfigureServices(IServiceCollection services)  
         {  
           services.AddTransient<IBankRepository, BankRepository>();    
         }  
Step-5
We need to design our Razor like below to get the input of IFSC code and after click the submit it gives us output.
     @model BankDetails_IFSC.Models.BankDetails  
     @{  
       ViewData["Title"] = "Home Page";  
     }  
     <style type="text/css">  
       .table tr td {  
         text-align:left;  
       }  
     </style>  
     <form asp-action="Index" role="form">  
       <div class="text-center">  
         <div class="container">  
           <div class="row">  
             <div class="col-md-9">  
               <input type="text" id="IFSC" name="IFSC" class="form-control" placeholder="IFSC Code" value="@ViewBag.IFSC" />  
             </div>  
             <div class="col-md-1">  
               <input type="submit" name="Submit" id="btnSubmit" class="btn btn-primary" />  
             </div>  
           </div>  
           <div class="clearfix">&nbsp;</div>  
           <div class="row">  
             @if (Model != null)  
             {  
               <table class="table table-bordered">  
                 <tbody>  
                   <tr>  
                     <td>@Html.DisplayNameFor(model => model.bank)</td>  
                     <td>@Html.DisplayFor(model => model.bank)</td>  
                   </tr>  
                   <tr>  
                     <td>@Html.DisplayNameFor(model => model.IFSC)</td>  
                     <td> @Html.DisplayFor(model => model.IFSC)</td>  
                   </tr>  
                   <tr>  
                     <td>@Html.DisplayNameFor(model => model.MICR)</td>  
                     <td> @Html.DisplayFor(model => model.MICR)</td>  
                   </tr>  
                   <tr>  
                     <td> @Html.DisplayNameFor(model => model.branch)</td>  
                     <td> @Html.DisplayFor(model => model.branch)</td>  
                   </tr>  
                   <tr>  
                     <td> @Html.DisplayNameFor(model => model.Address)</td>  
                     <td> @Html.DisplayFor(model => model.Address)</td>  
                   </tr>  
                   <tr>  
                     <td> @Html.DisplayNameFor(model => model.Contact)</td>  
                     <td> @Html.DisplayFor(model => model.Contact)</td>  
                   </tr>  
                   <tr>  
                     <td>  @Html.DisplayNameFor(model => model.City)</td>  
                     <td> @Html.DisplayFor(model => model.City)</td>  
                   </tr>  
                   <tr>  
                     <td> @Html.DisplayNameFor(model => model.District)</td>  
                     <td> @Html.DisplayFor(model => model.District)</td>  
                   </tr>  
                   <tr>  
                     <td> @Html.DisplayNameFor(model => model.State)</td>  
                     <td> @Html.DisplayFor(model => model.State)</td>  
                   </tr>  
                 </tbody>  
               </table>  
             }  
           </div>  
         </div>  
       </div>  
       </form>  
Step-6
 Then add the method in HomeController class to call the dependency like below
    public class HomeController : Controller  
       {  
         IBankRepository _bankrepository;  
         public HomeController(IBankRepository bankRepository)  
         {  
           ViewBag.IFSC = null;  
           _bankrepository = bankRepository;  
         }  
         public IActionResult Index()  
         {  
           return View();  
         }  
         [HttpPost]  
         public async Task<IActionResult> Index(string IFSC)  
         {  
           BankDetails bankdetails = _bankrepository.GetBankDetails(IFSC);  
           ViewBag.IFSC = IFSC;  
           return View(bankdetails);   
         }  
       } 
Step-7
 Finally run the application and enter any valid IFSC code of your known then click the button. It will return all the information of bank details and print the result as below.

</> Find the Source Code in Github.com/CoreProgramm/

Summary
   In this tutorial we discussed how to Consume Restful API to get the bank details using IFSC Code in .NET Core. If have any question related to this topic then give your feedback.


Post a Comment

Previous Post Next Post