About Me

My photo
Northglenn, Colorado, United States
I'm primarily a BI Developer on the Microsoft stack. I do sometimes touch upon other Microsoft stacks ( web development, application development, and sql server development).

Tuesday, March 05, 2013

Reminder: Setting up MVC4 Async Service in Controller

A quick reminder: I call a Json formatted file as a Service, that I placed in the App_Data folder. I then pull the contents needed using AsyncController.


Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using MadLibs.Models;
  7. using System.Net.Http;
  8. using Newtonsoft.Json;
  9.  
  10.  
  11. namespace MadLibs.Controllers
  12. {
  13.     public class MadLibController : AsyncController
  14.     {
  15.         //
  16.         // GET: /MadLib/
  17.  
  18.         public ActionResult StoryService()
  19.         {
  20.             return this.File("~/App_Data/StoriesDB.json", "application/json");
  21.         }
  22.  
  23.         public void IndexAsync()
  24.         {
  25.             String uri = Url.Action("StoryService", "MadLib", null, Request.Url.Scheme);
  26.             HttpClient client = new HttpClient();
  27.             var response = client.GetStringAsync(uri).Result;
  28.  
  29.             AsyncManager.Parameters["stories"] = JsonConvert.DeserializeObject<List<Story>>(response);
  30.         }
  31.  
  32.         public ActionResult IndexCompleted(List<Story> stories)
  33.         {
  34.             return View(stories);
  35.         }
  36.     }
  37. }

No comments: