Example #1
0
        /// <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)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            PathString matchedPath;
            PathString remainingPath;

            if (context.Request.Path.StartsWithSegments(_options.PathMatch, out matchedPath, out remainingPath))
            {
                // Update the path
                var path     = context.Request.Path;
                var pathBase = context.Request.PathBase;
                context.Request.PathBase = pathBase.Add(matchedPath);
                context.Request.Path     = remainingPath;

                try
                {
                    await _options.Branch(context);
                }
                finally
                {
                    context.Request.PathBase = pathBase;
                    context.Request.Path     = path;
                }
            }
            else
            {
                await _next(context);
            }
        }
        /// <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)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (context.Request.Path.StartsWithSegments(_options.PathMatch, out var matchedPath, out var remainingPath))
            {
                var path     = context.Request.Path;
                var pathBase = context.Request.PathBase;

                if (!_options.PreserveMatchedPathSegment)
                {
                    // Update the path
                    context.Request.PathBase = pathBase.Add(matchedPath);
                    context.Request.Path     = remainingPath;
                }

                try
                {
                    await _options.Branch(context);
                }
                finally
                {
                    if (!_options.PreserveMatchedPathSegment)
                    {
                        context.Request.PathBase = pathBase;
                        context.Request.Path     = path;
                    }
                }
            }