/// <summary> /// 尝试根据方法的修饰属性来构造IActionResult实例 /// </summary> /// <param name="result">Action的执行结果</param> /// <param name="actionAttr">Action方法上的ActionAttribute实例</param> /// <returns></returns> private IActionResult ObjectToResult(object result, ActionAttribute actionAttr) { IActionResult actionResult = null; SerializeFormat format = actionAttr.OutFormat; // 先判断是不是由客户端指定的自动模式,如果是就解析客户端需要的序列化格式 if (format == SerializeFormat.Auto) { // 如果是自动响应,那么就根据请求头的指定的方式来决定 string expectFormat = this.HttpContext.Request.Headers["X-Result-Format"]; if (string.IsNullOrEmpty(expectFormat) == false) { SerializeFormat outFormat; if (Enum.TryParse <SerializeFormat>(expectFormat, true, out outFormat)) { format = outFormat; } } } // 根据已指定的序列化格式包装具体的IActionResult实例 if (format == SerializeFormat.Json) { actionResult = new JsonResult(result); } else if (format == SerializeFormat.Json2) { actionResult = new JsonResult(result, true); } else if (format == SerializeFormat.Xml) { actionResult = new XmlResult(result); } else if (format == SerializeFormat.Text) { actionResult = new TextResult(result); } else if (format == SerializeFormat.Form) { string text = FormDataCollection.Create(result).ToString(); actionResult = new TextResult(text); } // 无法构造出IActionResult实例,就交给ActionProcessor来处理 if (actionResult == null) { ActionHelper actionProcessor = ObjectFactory.New <ActionHelper>(); actionResult = actionProcessor.ObjectToResult(result); } return(actionResult); }
public void Test_FormDataCollection_object_ToString() { var data = new { a = 1, b = 2, c = "xyz中文汉字" }; FormDataCollection form = FormDataCollection.Create(data); string actual = form.ToString(); Assert.AreEqual("a=1&b=2&c=xyz%e4%b8%ad%e6%96%87%e6%b1%89%e5%ad%97", actual); }
public void Test_FormDataCollection_IDictionary_ToString() { Hashtable table = new Hashtable(); table["a"] = 1; table["b"] = 2; table["c"] = "xyz中文汉字"; FormDataCollection form = FormDataCollection.Create(table); string actual = form.ToString(); Assert.AreEqual("a=1&b=2&c=xyz%e4%b8%ad%e6%96%87%e6%b1%89%e5%ad%97", actual); }
public void Test_FormDataCollection_NameValueCollection_ToString() { NameValueCollection collection = new NameValueCollection(); collection["a"] = "1"; collection["b"] = "2"; // 允许一个名称对应多个值 collection["c"] = "xyz中文汉字"; collection.Add("c", "789"); FormDataCollection form = FormDataCollection.Create(collection); string actual = form.ToString(); Console.WriteLine(actual); Assert.AreEqual("a=1&b=2&c=xyz%e4%b8%ad%e6%96%87%e6%b1%89%e5%ad%97&c=789", actual); }