public override bool TryFindContent(PublishedContentRequest contentRequest) { // eg / or /path/to/whatever var url = contentRequest.Uri.GetAbsolutePathDecoded(); var mdRoot = "/" + MarkdownLogic.BaseUrl; if (url.StartsWith("/projects/umbraco-pro/contour/documentation")) mdRoot = "/projects"; // ensure it's a md url if (url.StartsWith(mdRoot) == false) return false; // not for us // find the root content var node = FindContent(contentRequest, mdRoot); if (node == null) return false; // kill those old urls foreach (var s in new []{ "master", "v480" }) if (url.StartsWith(mdRoot + "/" + s)) { url = url.Replace(mdRoot + "/" + s, mdRoot); contentRequest.SetRedirectPermanent(url); return true; } // find the md file var mdFilepath = FindMarkdownFile(url); if (mdFilepath == null) { // clear the published content (that was set by FindContent) to cause a 404, and in // both case return 'true' because there's no point other finders try to handle the request contentRequest.PublishedContent = null; return true; } // set the context vars var httpContext = contentRequest.RoutingContext.UmbracoContext.HttpContext; httpContext.Items[MarkdownLogic.MarkdownPathKey] = mdFilepath; httpContext.Items["topicTitle"] = string.Join(" - ", httpContext.Request.RawUrl .Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries) .Skip(1) .Reverse()); // override the template const string altTemplate = "DocumentationSubpage"; var templateIsSet = contentRequest.TrySetTemplate(altTemplate); //httpContext.Trace.Write("Markdown Files Handler", // string.Format("Template changed to: '{0}' is {1}", altTemplate, templateIsSet)); // be happy return true; }
public bool TryFindContent(PublishedContentRequest request) { //Get the requested URL path + query var path = request.Uri.PathAndQuery.ToLower(); //Check the table var matchedRedirect = RedirectRepository.FindRedirect(path); if (matchedRedirect == null) return false; //Found one, set the 301 redirect on the request and return request.SetRedirectPermanent(matchedRedirect.NewUrl); return true; }
/// <summary> /// Tries to find and assign an Umbraco document to a <c>PublishedContentRequest</c>. /// </summary> /// <param name="contentRequest">The <c>PublishedContentRequest</c>.</param> /// <returns>A value indicating whether an Umbraco document was found and assigned.</returns> /// <remarks>Optionally, can also assign the template or anything else on the document request, although that is not required.</remarks> public bool TryFindContent(PublishedContentRequest contentRequest) { var route = contentRequest.HasDomain ? contentRequest.UmbracoDomain.RootContentId + DomainHelper.PathRelativeToDomain(contentRequest.DomainUri, contentRequest.Uri.GetAbsolutePathDecoded()) : contentRequest.Uri.GetAbsolutePathDecoded(); var service = contentRequest.RoutingContext.UmbracoContext.Application.Services.RedirectUrlService; var redirectUrl = service.GetMostRecentRedirectUrl(route); if (redirectUrl == null) { LogHelper.Debug <ContentFinderByRedirectUrl>("No match for route: \"{0}\".", () => route); return(false); } var content = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectUrl.ContentId); var url = content == null ? "#" : content.Url; if (url.StartsWith("#")) { LogHelper.Debug <ContentFinderByRedirectUrl>("Route \"{0}\" matches content {1} which has no url.", () => route, () => redirectUrl.ContentId); return(false); } // Apending any querystring from the incoming request to the redirect url. url = string.IsNullOrEmpty(contentRequest.Uri.Query) ? url : url + contentRequest.Uri.Query; LogHelper.Debug <ContentFinderByRedirectUrl>("Route \"{0}\" matches content {1} with url \"{2}\", redirecting.", () => route, () => content.Id, () => url); // From: http://stackoverflow.com/a/22468386/5018 // See http://issues.umbraco.org/issue/U4-8361#comment=67-30532 // Setting automatic 301 redirects to not be cached because browsers cache these very aggressively which then leads // to problems if you rename a page back to it's original name or create a new page with the original name contentRequest.Cacheability = HttpCacheability.NoCache; contentRequest.CacheExtensions = new List <string> { "no-store, must-revalidate" }; contentRequest.Headers = new Dictionary <string, string> { { "Pragma", "no-cache" }, { "Expires", "0" } }; contentRequest.SetRedirectPermanent(url); return(true); }