public IActionResult OnPost() { var jokeEndpoint = _configuration.GetValue <string>("JokeEndpoint"); if (!IsSearch) { AbstractJoke joke = new RandomJoke(new RestSharpClient(jokeEndpoint)); var response = joke.TellMeAJoke <RandomResponse, string>(""); return(new JsonResult(response)); } else { AbstractJoke joke = new SearchJoke(new RestSharpClient(jokeEndpoint)); var response = joke.TellMeAJoke <SearchResponse, SearchRequest>(new SearchRequest { term = SearchText, limit = 30 }); if (!string.IsNullOrEmpty(SearchText)) { foreach (var item in response.results) { item.joke = item.joke.Replace(SearchText, $"<b><i>{SearchText}</i></b>", StringComparison.OrdinalIgnoreCase); } } var jokeGroupsResponse = from jokeDto in response.results let wordCount = jokeDto.joke.Split().Length let jokeGroup = wordCount < 10 ? "Short" : wordCount < 20 ? "Medium" : "Long" orderby wordCount descending group jokeDto by jokeGroup into g select new KeyValuePair <string, IEnumerable <string> >(g.Key, g.Select(x => x.joke)); return(new JsonResult(jokeGroupsResponse)); } }
private void cboCategories_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selectedcategory = cboCategories.SelectedItem; if (selectedcategory == "All") { string jokeurl = "https://api.chucknorris.io/jokes/random"; Joke RandomJoke; using (var client = new HttpClient()) { string jsonres = client.GetStringAsync(jokeurl).Result; RandomJoke = JsonConvert.DeserializeObject <Joke>(jsonres); } txtJoke.Text = RandomJoke.ToString(); } else { string jokeurl = "https://api.chucknorris.io/jokes/random?category=" + selectedcategory; Joke NewJoke; using (var client = new HttpClient()) { string json = client.GetStringAsync(jokeurl).Result; NewJoke = JsonConvert.DeserializeObject <Joke>(json); } txtJoke.Text = NewJoke.ToString(); } }