Beispiel #1
0
        /// <summary>
        /// 尝试根据方法的修饰属性来构造IActionResult实例
        /// </summary>
        /// <param name="result"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        private IActionResult ObjectToActionResult(object result, SerializeFormat format)
        {
            IActionResult actionResult = null;

            if (format == SerializeFormat.AUTO)
            {
                // 如果是自动响应,那么就根据请求头的指定的方式来决定
                string expectFormat = this.HttpContext.Request.Headers["Result-Format"];

                if (string.IsNullOrEmpty(expectFormat) == false)
                {
                    SerializeFormat f2;
                    if (Enum.TryParse <SerializeFormat>(expectFormat.ToUpper(), out f2))
                    {
                        format = f2;
                    }
                }
            }


            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.FORM)
            {
                string text = FormDataProvider.Serialize(result).ToString();
                actionResult = new TextResult(text);
            }


            // 无法构造出IActionResult实例,就按字符串形式输出
            if (actionResult == null)
            {
                actionResult = new TextResult(result);
            }

            return(actionResult);
        }
		/// <summary>
		/// 尝试根据方法的修饰属性来构造IActionResult实例
		/// </summary>
		/// <param name="result"></param>
		/// <param name="format"></param>
		/// <returns></returns>
		private IActionResult ObjectToActionResult(object result, SerializeFormat format)
		{
			IActionResult actionResult = null;

			if( format == SerializeFormat.AUTO ) {
				// 如果是自动响应,那么就根据请求头的指定的方式来决定
				string expectFormat = this.HttpContext.Request.Headers["Result-Format"];

				if( string.IsNullOrEmpty(expectFormat) == false ) {
					SerializeFormat f2;
					if( Enum.TryParse<SerializeFormat>(expectFormat.ToUpper(), out f2) )
						format = f2;
				}
			}


			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.FORM ) {
				string text = FormDataProvider.Serialize(result).ToString();
				actionResult = new TextResult(text);
			}


			// 无法构造出IActionResult实例,就按字符串形式输出
			if( actionResult == null )
				actionResult = new TextResult(result);

			return actionResult;
		}