Exemple #1
0
        /// <summary>
        /// Called when an unhandled <see cref="ControllerActionException"/> is thrown.
        ///
        /// The default implementation will write a simple HTML response describing the error.
        /// </summary>
        /// <param name="e">The exception that was thrown.</param>
        protected virtual void OnUnhandledException(ControllerActionException e)
        {
            Response.StatusCode = (int)e.StatusCode;

            var title = $"{(int) e.StatusCode}: {e.StatusCode}";

            OnServiceHtml(new html
            {
                new head
                {
                    new title {
                        title
                    }
                },
                new body
                {
                    new h2 {
                        title
                    },
                    "Request:", nbsp, new code {
                        Request.Url.ToString()
                    }, br,
                    "Message:", nbsp, new code {
                        e.Message
                    }, br,
                    If((int)e.StatusCode >= 500 && (int)e.StatusCode < 600 && e.InnerException != null,
                       new code(style => "padding:8px;white-space:pre-wrap;display:block;")
                    {
                        e.ToString()
                    }
                       )
                }
            });
        }
Exemple #2
0
        public bool TryInvokeAction(Controller controller, HttpListenerRequest request)
        {
            var prefixMatch = controller.ControllerMatcher.Match(request.Url);

            Debug.Assert(prefixMatch.Success);

            ControllerActionException exception = null;

            for (var i = _actions.Count - 1; i >= 0; --i)
            {
                var bound = _actions[i];
                if (!IsMatchingHttpMethod(bound.HttpMethod, controller.HttpMethod))
                {
                    continue;
                }

                var match = bound.Matcher.Match(request.Url, prefixMatch.EndIndex);
                if (!match.Success)
                {
                    continue;
                }
                if (bound.MatchAllUrl && !IsMatchingAllUrl(match, request.Url))
                {
                    continue;
                }

                try
                {
                    controller.SetMatchedActionUrl(bound.Matcher, match);
                    bound.Action(controller);
                    return(true);
                }
                catch (ControllerActionException e)
                {
                    if (e.RequestHandled)
                    {
                        throw;
                    }
                    if (exception == null)
                    {
                        exception = e;
                    }
                }
            }

            if (exception != null)
            {
                throw exception;
            }
            return(false);
        }