Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public JsonResult JoinMailingList(string txtEmail)
- {
- // ...
- return new JsonResult()
- {
- Data = new { foo = "123", success = true }
- };
- }
- [JsonConvertible]
- public ActionResult JoinMailingList(string txtEmail)
- {
- // ...
- return new ObjectActionResult()
- {
- Data = new Foo(123, true)
- };
- }
- interface IFormatter {
- ActionResult Format(object o);
- }
- class HtmlFormatter : IFormatter {
- // ...
- }
- class JsonFormatter : IFormatter {
- // ...
- }
- class PlistFormatter : IFormatter {
- // ...
- }
- class XmlFormatter : IFormatter {
- // ...
- }
- public ActionResult JoinMailingList(string txtEmail, string format) {
- // ...
- return Formatter.For(format).Format(
- new { foo = "123", success = true }
- );
- }
- class ObjectFormatter : IFormatter {
- ActionResult Format(object o) {
- return new ObjectActionResult() {
- Data = o
- };
- }
- }
- protected static Dictionary<string, string> GetJsonProps(JsonResult result)
- {
- var properties = new Dictionary<string, string>();
- if (result != null && result.Data != null)
- {
- object o = result.Data;
- foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(o))
- properties.Add(prop.Name, prop.GetValue(o) as string);
- }
- return properties;
- }
- if (this.Request != null && this.Request.IsAjaxRequest())
- return Json(new { Message = "Success" });
- else
- return RedirectToAction("Some Action");
- [AcceptVerbs(HttpVerbs.Post)]
- public ActionResult ContactUsForm(FormCollection formData){
- // process formData ...
- var result = new JsonResult()
- {
- Data = new { success = true, message = "Thank you " + firstName }
- };
- return result;
- }
- var result = controller.ContactUsForm(formsData);
- if (result is JSonResult) {
- var json = result as JsonResult;
- bool success = json.GetProperty<bool>("success");
- string message = json.GetProperty<string>("message");
- // validate message and success are as expected
- }
- public static TSource GetProperty<TSource>(this JsonResult json, string propertyName)
- {
- if (propertyName == null)
- {
- throw new ArgumentNullException("propertyName");
- }
- if (json.Data == null)
- {
- throw new ArgumentNullException("JsonResult.Data"); // what exception should this be?
- }
- // reflection time!
- var propertyInfo = json.Data.GetType().GetProperty(propertyName);
- if (propertyInfo == null) {
- throw new ArgumentException("The property '" + propertyName + "' does not exist on class '" + json.Data.GetType() + "'");
- }
- if (propertyInfo.PropertyType != typeof(TSource))
- {
- throw new ArgumentException("The property '" + propertyName + "' was found on class '" + json.Data.GetType() + "' but was not of expected type '" + typeof(TSource).ToString());
- }
- var reflectedValue = (TSource) propertyInfo.GetValue(json.Data, null);
- return reflectedValue;
- }
- var jsonData = new RouteValueDictionary(result.Data);
- Assert.IsNotNull(jsonData);
- Assert.AreEqual(2,
- jsonData.Keys.Count);
- Assert.AreEqual("123",
- jsonData["foo"]);
- Assert.AreEqual(true,
- jsonData["success"]);
Add Comment
Please, Sign In to add comment