How could you optimize that code for faster response?
public ActionResult Details(int id)
{
DTOLibrary.PoliticianDTO politician = null;
using(var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:61442/api/");
var responseTask = client.GetAsync("politicians/" + id);
responseTask.Wait();
var result = responseTask.Result;
if(result.IsSuccessStatusCode)
{
// Install-Package Microsoft.AspNet.WebApi.Client -ProjectName Client
var readTask = result.Content.ReadAsAsync<DTOLibrary.PoliticianDTO>();
readTask.Wait();
politician = readTask.Result;
}
}
return View(politician);
}
[ResponseType(typeof(Politician))]
public IHttpActionResult PostPolitician(DTOLibrary.PoliticianDTO politician)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Politician p = new Politician();
p.fromDto();
db.politicians.Add(p);
db.SaveChanges();
return CreatedAtRoute("DefaultApi", new { id = politician.Id }, politician);
}
// PUT: api/Politicians/5
[ResponseType(typeof(void))]
public IHttpActionResult PutPolitician(int id, DTOLibrary.PoliticianDTO)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != politician.Id)
{
return BadRequest();
}
Politician p = db.stores.Find(politician.id);
db.Entry(p).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!PoliticianExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: Store/Create
[HttpPost]
public ActionResult Create([Bind(Include = "id, name, party")] DTOLibrary.PoliticianDTO politician)
{
using(var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:60183/api/");
var postTask = client.PostAsJsonAsync<DTOLibrary.PoliticianDTO>("politicians", politician);
postTask.Wait();
var result = postTask.Result;
if(result.IsSuccessStatusCode)
{
return RedirectToAction("Index");
}
}
return View();
}