/// <summary>
    /// Executes the middleware.
    /// </summary>
    /// <param name="context">The <see cref="HttpContext"/> for the current request.</param>
    /// <returns>A task that represents the execution of this middleware.</returns>
    public async Task Invoke(HttpContext context)
    {
        var statusCodeFeature = new StatusCodePagesFeature();

        context.Features.Set <IStatusCodePagesFeature>(statusCodeFeature);

        await _next(context);

        if (!statusCodeFeature.Enabled)
        {
            // Check if the feature is still available because other middleware (such as a web API written in MVC) could
            // have disabled the feature to prevent HTML status code responses from showing up to an API client.
            return;
        }

        // Do nothing if a response body has already been provided.
        if (context.Response.HasStarted ||
            context.Response.StatusCode < 400 ||
            context.Response.StatusCode >= 600 ||
            context.Response.ContentLength.HasValue ||
            !string.IsNullOrEmpty(context.Response.ContentType))
        {
            return;
        }

        var statusCodeContext = new StatusCodeContext(context, _options, _next);
        await _options.HandleAsync(statusCodeContext);
    }
        /// <inheritdoc/>
        protected override void Invoke(HttpContext context, RequestDelegate next)
        {
            var statusCodeFeature = new StatusCodePagesFeature();

            context.Features.Set(typeof(IStatusCodePagesFeature), statusCodeFeature);

            next(context);

            statusCodeFeature = (StatusCodePagesFeature)context.Features.Get(typeof(IStatusCodePagesFeature));
            if (!statusCodeFeature.Enabled)
            {
                // Check if the feature is still available because other middleware could
                // have disabled the feature to prevent HTML status code responses from showing up.
                return;
            }

            if (context.Response.StatusCode < 400 ||
                context.Response.StatusCode >= 600 ||
                context.Response.ContentLength > 0 ||
                !string.IsNullOrEmpty(context.Response.ContentType))
            {
                return;
            }

            _options.Handle(context);
        }
Example #3
0
        public async Task Invoke(HttpContext httpContext, ILogger <Logger> logger, Config config, TeknikEntities dbContext)
        {
            var statusCodeFeature = new StatusCodePagesFeature();

            httpContext.Features.Set <IStatusCodePagesFeature>(statusCodeFeature);

            Exception exception = null;

            try
            {
                await _next(httpContext);
            }
            catch (Exception ex)
            {
                exception = ex;
            }

            if (!statusCodeFeature.Enabled)
            {
                // Check if the feature is still available because other middleware (such as a web API written in MVC) could
                // have disabled the feature to prevent HTML status code responses from showing up to an API client.
                return;
            }

            // Do nothing if a response body has already been provided or not 404 response
            if (httpContext.Response.HasStarted)
            {
                return;
            }

            // Detect if there is a response code or exception occured
            if ((httpContext.Response.StatusCode >= 400 && httpContext.Response.StatusCode <= 600) || exception != null)
            {
                RouteData routeData = new RouteData();
                routeData.DataTokens.Add("area", "Error");
                routeData.Values.Add("controller", "Error");
                routeData.Routers.Add(_router);

                var context = new ControllerContext();
                context.HttpContext      = httpContext;
                context.RouteData        = routeData;
                context.ActionDescriptor = new Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor();

                ErrorController errorController = new ErrorController(logger, config, dbContext);
                errorController.ControllerContext = context;

                if (httpContext.Response.StatusCode == 500 || exception != null)
                {
                    await errorController.Http500(exception).ExecuteResultAsync(context);
                }
                else
                {
                    await errorController.HttpError(httpContext.Response.StatusCode).ExecuteResultAsync(context);
                }
            }
        }
        public async Task Invoke(HttpContext context)
        {
            var statusCodeFeature = new StatusCodePagesFeature();

            context.Features.Set <IStatusCodePagesFeature>(statusCodeFeature);

            await next(context);

            if (!statusCodeFeature.Enabled)
            {
                return;
            }
            if (context.Response.HasStarted)
            {
                return;
            }

            // 4XX, 5XX
            int status = context.Response.StatusCode;

            if (status < 400 || status >= 600)
            {
                return;
            }
            if (status == StatusCodes.Status400BadRequest)
            {
                throw new BadRequestException();
            }
            if (status == StatusCodes.Status401Unauthorized)
            {
                throw new UnauthorizedException();
            }
            if (status == StatusCodes.Status403Forbidden)
            {
                throw new ForbiddenException();
            }
            if (status == StatusCodes.Status404NotFound)
            {
                throw new NotFoundException();
            }
            if (status == StatusCodes.Status500InternalServerError)
            {
                throw new InternalServerErrorException();
            }
            if (status == StatusCodes.Status503ServiceUnavailable)
            {
                throw new ServiceUnavailableException();
            }
            throw new HttpException(status, string.Empty);
        }