コード例 #1
0
        /// <summary>
        /// This method is used when running in compatibility mode, maintained to preserve the behavior in 2.x
        /// </summary>
        internal static void LegacySetResponse(HttpRequest request, object result)
        {
            // use the existing response if already set (IBinder model)
            if (request.HttpContext.Items.TryGetValue(ScriptConstants.AzureFunctionsHttpResponseKey, out object existing) && existing is IActionResult)
            {
                return;
            }

            if (!(result is IActionResult actionResult))
            {
                if (result is Stream)
                {
                    // for script language functions (e.g. PowerShell, BAT, etc.) the value
                    // will be a Stream which we need to convert to string
                    FunctionBinding.ConvertStreamToValue((Stream)result, DataType.String, ref result);
                    actionResult = CreateResult(request, result);
                }
                else if (result is JObject)
                {
                    actionResult = CreateResult(request, result);
                }
                else if (isActionResultHandlingEnabled.Value && result is IConvertToActionResult actionResultConvertible)
                {
                    // Convert ActionResult<T> to ActionResult
                    actionResult = actionResultConvertible.Convert();
                }
                else
                {
                    actionResult = CreateObjectResult(result);
                }
            }

            request.HttpContext.Items[ScriptConstants.AzureFunctionsHttpResponseKey] = actionResult;
        }
コード例 #2
0
        internal static void SetResponse(HttpRequest request, object result)
        {
            // use the existing response if already set (IBinder model)
            if (request.HttpContext.Items.TryGetValue(ScriptConstants.AzureFunctionsHttpResponseKey, out object existing) && existing is IActionResult)
            {
                return;
            }

            IActionResult actionResult = result as IActionResult;

            if (actionResult == null)
            {
                if (result is Stream)
                {
                    // for script language functions (e.g. PowerShell, BAT, etc.) the value
                    // will be a Stream which we need to convert to string
                    FunctionBinding.ConvertStreamToValue((Stream)result, DataType.String, ref result);
                    actionResult = CreateResult(request, result);
                }
                else if (result is JObject)
                {
                    actionResult = CreateResult(request, result);
                }
                else if (isActionResultHandlingEnabled && result is IConvertToActionResult convertable)
                {
                    // Convert ActionResult<T> to ActionResult
                    actionResult = convertable.Convert();
                }
                else
                {
                    var objectResult = new ObjectResult(result);

                    if (result is System.Net.Http.HttpResponseMessage)
                    {
                        // To maintain backwards compatibility, if the type returned is an
                        // instance of an HttpResponseMessage, add the appropriate formatter to
                        // handle the response
                        objectResult.Formatters.Add(new HttpResponseMessageOutputFormatter());
                    }

                    actionResult = objectResult;
                }
            }

            request.HttpContext.Items[ScriptConstants.AzureFunctionsHttpResponseKey] = actionResult;
        }