code_junkie

Should my MVC controller really know about JSON

Nov 13th, 2011
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. public JsonResult JoinMailingList(string txtEmail)
  2. {
  3. // ...
  4.  
  5. return new JsonResult()
  6. {
  7. Data = new { foo = "123", success = true }
  8. };
  9. }
  10.  
  11. [JsonConvertible]
  12. public ActionResult JoinMailingList(string txtEmail)
  13. {
  14. // ...
  15.  
  16. return new ObjectActionResult()
  17. {
  18. Data = new Foo(123, true)
  19. };
  20. }
  21.  
  22. interface IFormatter {
  23. ActionResult Format(object o);
  24. }
  25. class HtmlFormatter : IFormatter {
  26. // ...
  27. }
  28. class JsonFormatter : IFormatter {
  29. // ...
  30. }
  31. class PlistFormatter : IFormatter {
  32. // ...
  33. }
  34. class XmlFormatter : IFormatter {
  35. // ...
  36. }
  37.  
  38. public ActionResult JoinMailingList(string txtEmail, string format) {
  39. // ...
  40. return Formatter.For(format).Format(
  41. new { foo = "123", success = true }
  42. );
  43. }
  44.  
  45. class ObjectFormatter : IFormatter {
  46. ActionResult Format(object o) {
  47. return new ObjectActionResult() {
  48. Data = o
  49. };
  50. }
  51. }
  52.  
  53. protected static Dictionary<string, string> GetJsonProps(JsonResult result)
  54. {
  55. var properties = new Dictionary<string, string>();
  56. if (result != null && result.Data != null)
  57. {
  58. object o = result.Data;
  59. foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(o))
  60. properties.Add(prop.Name, prop.GetValue(o) as string);
  61. }
  62. return properties;
  63. }
  64.  
  65. if (this.Request != null && this.Request.IsAjaxRequest())
  66. return Json(new { Message = "Success" });
  67. else
  68. return RedirectToAction("Some Action");
  69.  
  70. [AcceptVerbs(HttpVerbs.Post)]
  71. public ActionResult ContactUsForm(FormCollection formData){
  72.  
  73. // process formData ...
  74.  
  75. var result = new JsonResult()
  76. {
  77. Data = new { success = true, message = "Thank you " + firstName }
  78. };
  79.  
  80. return result;
  81. }
  82.  
  83. var result = controller.ContactUsForm(formsData);
  84. if (result is JSonResult) {
  85.  
  86. var json = result as JsonResult;
  87. bool success = json.GetProperty<bool>("success");
  88. string message = json.GetProperty<string>("message");
  89.  
  90. // validate message and success are as expected
  91. }
  92.  
  93. public static TSource GetProperty<TSource>(this JsonResult json, string propertyName)
  94. {
  95. if (propertyName == null)
  96. {
  97. throw new ArgumentNullException("propertyName");
  98. }
  99.  
  100. if (json.Data == null)
  101. {
  102. throw new ArgumentNullException("JsonResult.Data"); // what exception should this be?
  103. }
  104.  
  105. // reflection time!
  106. var propertyInfo = json.Data.GetType().GetProperty(propertyName);
  107.  
  108. if (propertyInfo == null) {
  109. throw new ArgumentException("The property '" + propertyName + "' does not exist on class '" + json.Data.GetType() + "'");
  110. }
  111.  
  112. if (propertyInfo.PropertyType != typeof(TSource))
  113. {
  114. throw new ArgumentException("The property '" + propertyName + "' was found on class '" + json.Data.GetType() + "' but was not of expected type '" + typeof(TSource).ToString());
  115. }
  116.  
  117. var reflectedValue = (TSource) propertyInfo.GetValue(json.Data, null);
  118. return reflectedValue;
  119. }
  120.  
  121. var jsonData = new RouteValueDictionary(result.Data);
  122. Assert.IsNotNull(jsonData);
  123.  
  124. Assert.AreEqual(2,
  125. jsonData.Keys.Count);
  126.  
  127. Assert.AreEqual("123",
  128. jsonData["foo"]);
  129.  
  130. Assert.AreEqual(true,
  131. jsonData["success"]);
Add Comment
Please, Sign In to add comment