mardi 12 mai 2020

ASP.Net CORE request an endpoint and parse random string from the endpoint

this is a first time I'll be using asp.net core so started working on a very simple project to get hands on. The following public API generates a large JSON response of famous quotes. I am trying to query this end point and randomly display one of the quotes.

https://type.fit/api/quotes

Here is the code I'm working on

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Net.Http;

     namespace QuoteAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class QuoteGenerator : ControllerBase
{
    [HttpGet]
    public static char Main()
    {
        string quotations = "https://type.fit/api/quotes";
        using (HttpClient client = new HttpClient())
        {
            Random rand = new Random();
            int r = rand.Next(quotations.Length);
            var quotation = quotations[r];
            return quotation;       

            // return  await client.GetStringAsync(quotations);
        }
    }


   }


 }

While it doesn't show any errors in the code, when I run this code, it says no localhost page found. I'm pretty sure there are 100's of mistake in my code. I tried all over the internet for couple of hours but couldn't find any solution to my problem probably because it is a very simple problem. Thank you for your time!




Aucun commentaire:

Enregistrer un commentaire