Example #1
0
        private static IEnumerable <string> AppendBaseHrefToOptions(string currentDirectory, IEnumerable <string> optionsList)
        {
            var ngAppSettings = NgMiddlewareHelper.GetAllNgAppSettings(currentDirectory);

            // TODO AF20170929. Check the app names/numbers in optionsList against the app names in .angular-cli.json.
            if (optionsList.Count() > ngAppSettings.Count())
            {
                throw new ArgumentOutOfRangeException(ErrorAppCount);
            }
            // TODO AF20170929. We match apps by position here. Match the app in optionsList with apps in .angular-cli.json by name/number, i.e. --app=app0. See +https://github.com/angular/angular-cli/wiki/stories-multiple-apps
            var newOptionsList = optionsList.Select((optionsLine, index) =>
            {
                var baseHref = ngAppSettings
                               .Where(i => i.AppIndex == index)
                               .Select(i => i.BaseHref)
                               .FirstOrDefault()
                ;
                if (String.IsNullOrWhiteSpace(baseHref))
                {
                    throw new Exception(String.Format(NgMiddlewareHelper.ErrorNoBaseHrefInAngularCliJson, index));
                }

                return(optionsLine + " --base-href " + baseHref); // ng tolerates extra whitespaces in command line.
            })
                                 .ToList();

            return(newOptionsList);
        }
        public NgRouteMiddleware(RequestDelegate next, IHostingEnvironment hostingEnv)
        {
            // Store the next middleware in the request processing chain.
            this.next = next ?? throw new ArgumentNullException(nameof(next));

            if (hostingEnv == null)
            {
                throw new ArgumentNullException(nameof(hostingEnv));
            }

            var contentRootPath = hostingEnv.ContentRootPath;
            var webRootPath     = hostingEnv.WebRootPath;

            var ngAppSettings = NgMiddlewareHelper.GetAllNgAppSettings(contentRootPath)
                                .Where(i => !String.IsNullOrWhiteSpace(i.BaseHref));

            if (!ngAppSettings.Any())
            {
                throw new Exception(String.Format(NgMiddlewareHelper.ErrorNoBaseHrefInAngularCliJson, 0));
            }

            this.pathPrefixToFilePathMap = ngAppSettings
                                           .Select(i => new
            {
                // Argument of PathString.StartsWithSegments() must not have a trailing slash.
                RequestPathSegment = i.BaseHref == "/" ? i.BaseHref : i.BaseHref.TrimEnd('/'),
                // Slashes are acceptable in the middle and at the end, but not at the start of a path segment.
                IndexFilePath = Path.Combine(webRootPath, i.BaseHref.TrimStart('/'), i.IndexFileName),
            })
                                           .Select(i => new KeyValuePair <string, string>(i.RequestPathSegment, i.IndexFilePath))
                                           .ToList()
            ;
            this.pathPrefixToFilePathMap.ForEach(i =>
            {
                if (!File.Exists(i.Value))
                {
                    throw new Exception(String.Format(ErrorNgIndexFileNotFound, i.Value));
                }
            });
        }