public HttpResponseMessage SaveRedirect(MappingModel model)
        {
            var map = new Mapping();

            map.SourceUrl = model.SourceUrl.Trim();
            if (!string.IsNullOrEmpty(model.TargetUrl))
            {
                map.TargetUrl = model.TargetUrl;
            }
            else if (model.TargetTabId > 0)
            {
                map.TargetTabId = model.TargetTabId;
                map.TargetUrl   = DotNetNuke.Common.Globals.NavigateURL(map.TargetTabId, PortalSettings, "");
            }
            else
            {
                // keep giving 404
                map.EnableLogging = false;
            }
            if (map != null)
            {
                map.UseRegex = false;
                var cfg = RedirectConfig.Instance;
                cfg.Mappings.Add(map);
                cfg.ToFile(Common.RedirectConfigFile());
                RedirectConfig.Reload(PortalSettings.PortalId);
            }

            // set handledon/handledby in table
            RedirectController.SetHandledUrl(model.SourceUrl);

            return(Request.CreateResponse(HttpStatusCode.OK, new {}));
        }
Example #2
0
        public ActionResult HandleRequest(string page = "")
        {
            // Convert to lowercase
            page = page.ToLowerInvariant();

            ActionResult result;

            // Handle any site specific redirects
            if ((result = RedirectConfig.HandleRedirects(page)) != null)
            {
                return(result);
            }

            // Serve the request
            if (IsStaticFileRequest(page))
            {
                result = ServeStaticFile(page);
            }
            else
            {
                result = ServeContentPage(page);
            }

            // Set up the response cache headers
            if (!(result is HttpNotFoundResult))
            {
                Response.Cache.SetETagFromFileDependencies();
                Response.Cache.SetLastModifiedFromFileDependencies();
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetMaxAge(new TimeSpan(0, 30, 0));
                Response.Cache.SetSlidingExpiration(true);
            }

            return(result);
        }
        public HttpResponseMessage SaveMapping(MappingModel model) // string id, bool useRegex, string sourceUrl, string targetUrl, int targetTabId)
        {
            var map = RedirectConfig.Instance.Mappings.FirstOrDefault(m => m.Id == model.Id);

            if (!string.IsNullOrEmpty(model.Id) && map == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            if (map == null && string.IsNullOrEmpty(model.Id))
            {
                map    = new Mapping();
                map.Id = Guid.NewGuid().ToString();
                RedirectConfig.Instance.Mappings.Add(map);
            }

            map.StatusCode    = model.StatusCode;
            map.SourceUrl     = model.SourceUrl.Trim();
            map.UseRegex      = model.UseRegex;
            map.EnableLogging = model.EnableLogging;
            if (!string.IsNullOrEmpty(model.TargetUrl))
            {
                map.TargetTabId = Null.NullInteger;
                map.TargetUrl   = model.TargetUrl;
            }
            else if (model.TargetTabId > 0)
            {
                map.TargetTabId = model.TargetTabId;
                map.TargetUrl   = DotNetNuke.Common.Globals.NavigateURL(map.TargetTabId, PortalSettings, "");
            }
            else if (model.TargetTabId == -1) // matches with number in js file
            {
                map.TargetTabId = model.TargetTabId;
                map.TargetUrl   = "";
            }
            else if (model.TargetTabId == -2) // matches with number in js file, meaning it needs to be deleted
            {
                // remove mapping
                RedirectConfig.Instance.Mappings.Remove(map);
                map = null;
            }

            RedirectConfig.Instance.ToFile(Common.RedirectConfigFile());
            RedirectConfig.Reload(PortalSettings.PortalId);

            // set handledon/handledby in table
            if (!model.UseRegex)
            {
                RedirectController.SetHandledUrl(model.SourceUrl);
            }

            return(Request.CreateResponse(HttpStatusCode.OK, map == null ? null : new MappingModel(map)));
        }
        static async ValueTask SaveRedirectAsync(Options options, RedirectConfig redirectConfig, Dictionary <string, ISet <Redirect> > redirectMap)
        {
            try
            {
                var startingCount = redirectConfig.Redirections.Count;
                redirectConfig.Redirections.AddRange(redirectMap.SelectMany(kvp => kvp.Value));
                var additions = redirectConfig.Redirections.Count - startingCount;
                var dir       = options.Directory.TraverseToFile(".openpublishing.redirection.json");
                var json      = redirectConfig.ToJson();
                var path      = Path.Combine(dir.FullName, ".openpublishing.redirection.json");

                await File.WriteAllTextAsync(path, json);

                FileType.Markdown.WriteLine($"Automatically applied {additions:#,#} redirects to the \".openpublishing.redirection.json\" file.");
            }
            catch (Exception ex) when(options.OutputWarnings)
            {
                ConsoleColor.DarkMagenta.WriteLine($"Unable to apply redirects. {ex.Message}");
            }
        }