Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Frederik 3 posts 73 karma points
    Apr 17, 2020 @ 14:07
    Frederik
    0

    Loading data from external site

    I need to load data from an external site. For various reason I can not load it using ajax. I therefore need to do it in C#.

    I have followed this guide: https://our.umbraco.com/documentation/reference/routing/custom-controllers

    And came up with the following code:

    public class HomeController : RenderMvcController
    {
        private static readonly HttpClient client = new HttpClient();
        public override ActionResult Index(ContentModel model)
        {
            var responseTask = client.GetStringAsync("https://www.alphavantage.co/query?function=GLOBAL_QUOTE&symbol=AAPL&apikey=XXXXX");
            string response = responseTask.Result;
    
            var result = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(response);
    
            ViewBag.StockPrice = result["Global Quote"]["05. price"];
            return base.Index(model);
        }
    }
    

    Is this the preferred way to do it? Or is there a better way?

    When doing it this way will the page be cached?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Apr 17, 2020 @ 16:42
    Alex Skrypnyk
    1

    Hi Frederik,

    The preferred way would be to add caching :) It will be probably quite slow to call external service each page load.

    Alex

  • Frederik 3 posts 73 karma points
    Apr 20, 2020 @ 06:34
    Frederik
    0

    What is the preferred way to do caching? Let say I want new content every 20 min.

    I have read https://our.umbraco.com/documentation/reference/cache/, but it is lacking information.

Please Sign in or register to post replies

Write your reply to:

Draft