protected async void btnSearch_ClickAsync(object sender, EventArgs e) { string searchTerm = SearchText.Value; if (string.IsNullOrWhiteSpace(searchTerm)) { rptResultTable.DataSource = null; rptResultTable.DataBind(); return; } List <ResultDisplayObject> resultList = new List <ResultDisplayObject>(); using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://localhost:8090/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); HttpResponseMessage response = await client.GetAsync("api/search?word=" + searchTerm); if (response.IsSuccessStatusCode) { resultList = await response.Content.ReadAsAsync <List <ResultDisplayObject> >(); rptResultTable.DataSource = resultList; rptResultTable.DataBind(); } else { ResultDisplayObject errorResult = new ResultDisplayObject() { Title = "ERROR", SearchEngine = "ERROR" }; resultList.Add(errorResult); rptResultTable.DataSource = resultList; rptResultTable.DataBind(); Console.WriteLine("Internal server Error"); } } }
public List <ResultDisplayObject> Get(string word) { List <string> resultStrings = new List <string>(); List <ResultDisplayObject> searchResults = new List <ResultDisplayObject>(); SearchEngineDbConnection db = new SearchEngineDbConnection(); // get exising results from db searchResults = db.GetQueryResults(word); // if this is a new search if (searchResults == null || searchResults.Count == 0) { SearchEngine se = new BingEngine(); searchResults = new List <ResultDisplayObject>(); resultStrings = se.GetResultsFromHtml(word); foreach (string str in resultStrings) { ResultDisplayObject listItem = new ResultDisplayObject() { SearchEngine = se.EngineName, Title = str }; searchResults.Add(listItem); } // add to db db.AddQueryResults(se.EngineName, word, resultStrings); // add to cache (redis) } return(searchResults); }