I have a problem with my Web Application. I managed to search through the jokes and display the ones that contain certain word (by using API https://icanhazdadjoke.com/), but I don't know how to display them in better way than this:
I have my async Task method that search jokes:
[HttpPost]
public async Task<List<string>> SearchJoke()
{
string searchedWord = Request.Form["word"];
var client = _httpClientFactory.CreateClient("API Client");
string url = client.BaseAddress + "search?term=" + searchedWord;
string responseBody = await client.GetStringAsync(url);
JokesModel jokes = JsonConvert.DeserializeObject<JokesModel>(responseBody);
List<string> list = new List<string>();
for (int i = 0; i < jokes.Results.Count; i++)
{
list.Add(jokes.Results[i].Joke);
}
return list;
}
And I just return a View like this:
public IActionResult Index()
{
return View();
}
My plan is to write something like this:
public IActionResult Index()
{
//check if the word was passed and redirect to the another View which displays jokes
// else show some message that the word is incorrect or there are no jokes contain that word
}
I tried checking if the word was passed and wrote another method:
[HttpPost]
public bool GetWord()
{
string searchedTerm = Request.Form["word"];
if (searchedTerm != null)
return true;
return false;
}
And do something like this:
public IActionResult Index()
{
if (GetWord())
{
return RedirectToAction(nameof(HomeController.SearchJoke), "Home");
//this is the page where I display joke in more readable way
}
return View();
}
But I got an error: "System.InvalidOperationException: „Incorrect Content-Type:" when I want to download word in GetWord() method.
Anybody can give me some advice or help me with this problem?
Aucun commentaire:
Enregistrer un commentaire