I'm creating an ASP.Net core MVC application to retrieve and display current currencies with their values in a user friendly way on my html page. I am using cryptocompare's api to obtain these values.
The Json response I get from the api is
{"EUR":{"BTC":0.0003188,"USD":1.14}}
EUR.cs
namespace ProjectFinanceHub.Models
{
public class EUR
{
public double BTC { get; set; }
public double USD { get; set; }
}
}
HomeController.cs Below I have removed my personal api key and replaced it 'your_api_here'
namespace ProjectFinanceHub.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public async Task<ActionResult> EUR()
{
var req = WebRequest.Create(@"https://min-api.cryptocompare.com/data/pricemulti?fsyms=EUR&tsyms=BTC,USD,EUR&api_key=your_api_here");
var r = await req.GetResponseAsync().ConfigureAwait(false);
var responseReader = new StreamReader(r.GetResponseStream());
var responseData = await responseReader.ReadToEndAsync();
var d = Newtonsoft.Json.JsonConvert.DeserializeObject<EUR>(responseData);
return View(d);
}
}
}
EUR.cshtml
@model ProjectFinanceHub.Models.EUR
<h2>@Model.BTC</h2> <!-- Should display 0.0003188 for example-->
<h3>@Model.USD</h3> <!-- Should display 1.13 for example-->
Currently I get values of 0 display for both BTC and USD, the expected values are that these values are the ones from the json response