﻿using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace WebPackAngular2TypeScript.Routing
{
    public class DeepLinkingMiddleware
    {
        public DeepLinkingMiddleware(RequestDelegate next,
            IHostingEnvironment hostingEnv,
            ILoggerFactory loggerFactory,
            DeepLinkingOptions options)
        {
            this.next = next;
            this.options = options;

            staticFileMiddleware = new StaticFileMiddleware(next, 
                hostingEnv, options.FileServerOptions.StaticFileOptions, loggerFactory);
        }

        public async Task Invoke(HttpContext context)
        {
            // try to resolve the request with default static file middleware
            await staticFileMiddleware.Invoke(context);

            if (context.Response.StatusCode == 404)
            {
                var redirectUrlPath = FindRedirection(context);

                if (redirectUrlPath != unresolvedPath)
                {
                    context.Request.Path = redirectUrlPath;

                    await staticFileMiddleware.Invoke(context);
                }
            }
        }

        protected virtual PathString FindRedirection(HttpContext context)
        {
            // route to root path when request was not resolved
            return options.RedirectUrlPath;
        }

        protected readonly DeepLinkingOptions options;
        protected readonly RequestDelegate next;
        protected readonly StaticFileMiddleware staticFileMiddleware;

        protected readonly PathString unresolvedPath = null;
    }
}

﻿using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.StaticFiles;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;

namespace WebPackAngular2TypeScript.Routing
{
    public class DeepLinkingMiddleware
    {
        public DeepLinkingMiddleware(RequestDelegate next,
            IHostingEnvironment hostingEnv,
            ILoggerFactory loggerFactory,
            DeepLinkingOptions options)
        {
            this.next = next;
            this.options = options;

            staticFileMiddleware = new StaticFileMiddleware(next, 
                hostingEnv, options.FileServerOptions.StaticFileOptions, loggerFactory);
        }

        public async Task Invoke(HttpContext context)
        {
            // try to resolve the request with default static file middleware
            await staticFileMiddleware.Invoke(context);

            if (context.Response.StatusCode == StatusCodes.Status404NotFound)
            {
                var redirectUrlPath = FindRedirection(context);

                if (redirectUrlPath != unresolvedPath)
                {
                    // if resolved, reset response as successful
                    context.Response.StatusCode = StatusCodes.Status200OK;
                    context.Request.Path = redirectUrlPath;

                    await staticFileMiddleware.Invoke(context);
                }
            }
        }

        protected virtual PathString FindRedirection(HttpContext context)
        {
            // route to root path when request was not resolved
            return options.RedirectUrlPath;
        }

        protected readonly DeepLinkingOptions options;
        protected readonly RequestDelegate next;
        protected readonly StaticFileMiddleware staticFileMiddleware;

        protected readonly PathString unresolvedPath = null;
    }
}

Fix spurious 404 response when deep linking to a client route
Fix spurious 404 response when deep linking to a client route
