Пример #1
0
        /// <summary>
        /// Converts a command result to an action result.
        /// </summary>
        /// <param name="commandResult">The source command result.</param>
        /// <param name="response">Http Response to write the result to.</param>
        /// <returns>
        /// Action result
        /// </returns>
        /// <exception cref="System.ArgumentNullException">commandResult</exception>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <remarks>
        /// The reason to use a separate command result at all, instead
        /// of simply using ActionResult is that the core library should not
        /// be Mvc dependant.
        /// </remarks>
        public static ActionResult ToActionResult(this CommandResult commandResult, HttpResponseBase response)
        {
            if (commandResult == null)
            {
                throw new ArgumentNullException("commandResult");
            }

            response.SetStoredRequestState(commandResult.StoredRequestState);

            switch (commandResult.HttpStatusCode)
            {
            case HttpStatusCode.SeeOther:
                return(new RedirectResult(commandResult.Location.OriginalString));

            case HttpStatusCode.OK:
                var result = new ContentResult()
                {
                    Content = commandResult.Content
                };

                if (!string.IsNullOrEmpty(commandResult.ContentType))
                {
                    result.ContentType = commandResult.ContentType;
                }

                return(result);

            default:
                throw new NotImplementedException();
            }
        }
Пример #2
0
        public static void Apply(this CommandResult commandResult, HttpResponseBase response)
        {
            if (commandResult == null)
            {
                throw new ArgumentNullException("commandResult");
            }

            if (response == null)
            {
                throw new ArgumentNullException("response");
            }

            response.Cache.SetCacheability((HttpCacheability)commandResult.Cacheability);

            response.SetStoredRequestState(commandResult.StoredRequestState);

            if (commandResult.HttpStatusCode == HttpStatusCode.SeeOther || commandResult.Location != null)
            {
                if (commandResult.Location == null)
                {
                    throw new InvalidOperationException("Missing Location on redirect.");
                }
                if (commandResult.HttpStatusCode != HttpStatusCode.SeeOther)
                {
                    throw new InvalidOperationException("Invalid HttpStatusCode for redirect, but Location is specified");
                }

                response.Redirect(commandResult.Location.OriginalString);
            }
            else
            {
                response.StatusCode = (int)commandResult.HttpStatusCode;
                response.ContentType = commandResult.ContentType;
                response.Write(commandResult.Content);

                response.End();
            }
        }