Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 480.8k

How to expose API endpoints in C# WebServices?

Oct 13 2020 7:29 AM
  1. // Model  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Runtime.Serialization;  
  6. using System.Web;  
  7.   
  8. namespace CoinJarAPI.Models  
  9. {  
  10.     [DataContract]  
  11.     public class CoinJarModel  
  12.     {  
  13.         [DataMember(Name = "volume")]  
  14.         public decimal Volume { getset; }  
  15.   
  16.         [DataMember(Name = "amount")]  
  17.         public decimal Amount { getset; }  
  18.   
  19.         [DataMember(Name = "getTotalAmount")]  
  20.         public decimal GetTotalAmount { getset; }  
  21.     }  
  22. }  
  23.   
  24. // Controller  
  25. using System;  
  26. using System.Collections.Generic;  
  27. using System.Linq;  
  28. using System.Net;  
  29. using System.Net.Http;  
  30. using System.Web.Http;  
  31.   
  32. using CoinJarAPI.Models;  
  33.   
  34. namespace CoinJarAPI.Controllers  
  35. {  
  36.     public class CoinJarController : ApiController  
  37.     {  
  38.         // GET: api/CoinJar  
  39.         public IEnumerable<CoinJarModel> Get()  
  40.         {  
  41.             var coinJarList = new List<CoinJarModel>();  
  42.             for (int i = 0; i < 10; i++)  
  43.             {  
  44.                 var coinjarModel = new CoinJarModel  
  45.                 {  
  46.                     // volume, Amount, GetTotalAmount.  
  47.                 };  
  48.             }  
  49.             return coinJarList;  
  50.         }  
  51.   
  52.         // GET: api/CoinJar/5  
  53.         public string Get(int id)  
  54.         {  
  55.             return "value";  
  56.         }  
  57.   
  58.     }  
  59. }  
  60. // Interface  
  61. using System;  
  62. using System.Collections.Generic;  
  63. using System.Linq;  
  64. using System.Text;  
  65. using System.Threading.Tasks;  
  66.   
  67. namespace CoinJarAPI.Interface  
  68. {  
  69.     interface ICoinJar  
  70.     {  
  71.         void AddCoin(ICoin coin);  
  72.         decimal GetTotalAmount();  
  73.         void Reset();  
  74.     }  
  75.   
  76.     public interface ICoin  
  77.     {  
  78.         decimal Amount { getset; }  
  79.         decimal Volume { getset; }  
  80.     }  
  81. }  
Hi Team
 
I want to expose 3 endpoints that will do this following  and this is what i have currently, question where do i implement these endpoints? Controller or Interface, do i have create a Model? Need some advice
 
Add a coin
Get the total amount of coins
Reset the coins
 

Answers (2)