How can do I work with StackOverflow API and HTTP request it?
This is my problem: I want to get first 1000 tags from Stack Exchange API, see their popularity and usage.
This is what I've done so far:
Created an ASP.NET MVC project
Added HTTP Client in
Startup.csfile:public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddHttpClient("stackoverflow", s => { s.BaseAddress = new Uri("https://api.stackexchange.com/2.3"); s.DefaultRequestHeaders.Add("client_id", "1"); s.DefaultRequestHeaders.Add("site", "stackoverflow"); s.DefaultRequestHeaders.Add("redirect_uri", "https://api.stackexchange.com/2.3/tags?"); }); }Created classes
Tagsand interfaceITagsRepository:public class Tags { public int Id { get; set; } public int Popularity { get; set; } public decimal Usage { get; set; } } public class TagsRepository : ITagsRepository { public Tags GetTag(int id) { // put logic in here } }I also have added a
TagsControllerinHomeController.cs:public class TagsController : Tags { private ITagsRepository _tagsRepository; public TagsController(ITagsRepository tagsRepository) { _tagsRepository = tagsRepository; } public IActionResult GetTag(int id) { Tags tag = _tagsRepository.GetTag(id); return View(tag); } }
My question is: how do I make my code run in index.cshtml and how do I make http requests that are customisable from client side?
TagsControllerdoesn't need to inherit fromTags, so remove that. I don't think it will cause a problem, it just doesn't make any sense.