private static IApplicationBuilder AddRewriteMiddleware(IApplicationBuilder app, IOptions <RewriteOptions>?options)
    {
        // Only use this path if there's a global router (in the 'WebApplication' case).
        if (app.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
        {
            return(app.Use(next =>
            {
                if (options is null)
                {
                    options = app.ApplicationServices.GetRequiredService <IOptions <RewriteOptions> >();
                }

                var webHostEnv = app.ApplicationServices.GetRequiredService <IWebHostEnvironment>();
                var loggerFactory = app.ApplicationServices.GetRequiredService <ILoggerFactory>();

                var newNext = RerouteHelper.Reroute(app, routeBuilder, next);
                options.Value.BranchedNext = newNext;

                return new RewriteMiddleware(next, webHostEnv, loggerFactory, options).Invoke;
            }));
        }

        if (options is null)
        {
            return(app.UseMiddleware <RewriteMiddleware>());
        }

        return(app.UseMiddleware <RewriteMiddleware>(options));
    }
    /// <summary>
    /// Adds a middleware that extracts the specified path base from request path and postpend it to the request path base.
    /// </summary>
    /// <param name="app">The <see cref="IApplicationBuilder"/> instance.</param>
    /// <param name="pathBase">The path base to extract.</param>
    /// <returns>The <see cref="IApplicationBuilder"/> instance.</returns>
    public static IApplicationBuilder UsePathBase(this IApplicationBuilder app, PathString pathBase)
    {
        if (app == null)
        {
            throw new ArgumentNullException(nameof(app));
        }

        // Strip trailing slashes
        pathBase = new PathString(pathBase.Value?.TrimEnd('/'));
        if (!pathBase.HasValue)
        {
            return(app);
        }

        // Only use this path if there's a global router (in the 'WebApplication' case).
        if (app.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
        {
            return(app.Use(next =>
            {
                var newNext = RerouteHelper.Reroute(app, routeBuilder, next);
                return new UsePathBaseMiddleware(newNext, pathBase).Invoke;
            }));
        }

        return(app.UseMiddleware <UsePathBaseMiddleware>(pathBase));
    }
Example #3
0
    /// <summary>
    /// Adds a StatusCodePages middleware to the pipeline. Specifies that the response body should be generated by
    /// re-executing the request pipeline using an alternate path. This path may contain a '{0}' placeholder of the status code.
    /// </summary>
    /// <param name="app"></param>
    /// <param name="pathFormat"></param>
    /// <param name="queryFormat"></param>
    /// <returns></returns>
    public static IApplicationBuilder UseStatusCodePagesWithReExecute(
        this IApplicationBuilder app,
        string pathFormat,
        string?queryFormat = null)
    {
        if (app == null)
        {
            throw new ArgumentNullException(nameof(app));
        }

        // Only use this path if there's a global router (in the 'WebApplication' case).
        if (app.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
        {
            return(app.Use(next =>
            {
                var newNext = RerouteHelper.Reroute(app, routeBuilder, next);
                return new StatusCodePagesMiddleware(next,
                                                     Options.Create(new StatusCodePagesOptions()
                {
                    HandleAsync = CreateHandler(pathFormat, queryFormat, newNext)
                })).Invoke;
            }));
        }

        return(app.UseStatusCodePages(CreateHandler(pathFormat, queryFormat)));
    }
    private static IApplicationBuilder SetExceptionHandlerMiddleware(IApplicationBuilder app, IOptions <ExceptionHandlerOptions>?options)
    {
        var problemDetailsService = app.ApplicationServices.GetService <IProblemDetailsService>();

        app.Properties["analysis.NextMiddlewareName"] = "Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware";

        // Only use this path if there's a global router (in the 'WebApplication' case).
        if (app.Properties.TryGetValue(RerouteHelper.GlobalRouteBuilderKey, out var routeBuilder) && routeBuilder is not null)
        {
            return(app.Use(next =>
            {
                var loggerFactory = app.ApplicationServices.GetRequiredService <ILoggerFactory>();
                var diagnosticListener = app.ApplicationServices.GetRequiredService <DiagnosticListener>();

                if (options is null)
                {
                    options = app.ApplicationServices.GetRequiredService <IOptions <ExceptionHandlerOptions> >();
                }

                if (!string.IsNullOrEmpty(options.Value.ExceptionHandlingPath) && options.Value.ExceptionHandler is null)
                {
                    var newNext = RerouteHelper.Reroute(app, routeBuilder, next);
                    // store the pipeline for the error case
                    options.Value.ExceptionHandler = newNext;
                }

                return new ExceptionHandlerMiddlewareImpl(next, loggerFactory, options, diagnosticListener, problemDetailsService).Invoke;
            }));
        }

        if (options is null)
        {
            return(app.UseMiddleware <ExceptionHandlerMiddlewareImpl>());
        }

        return(app.UseMiddleware <ExceptionHandlerMiddlewareImpl>(options));
    }