protected virtual bool HandleUmbracoRedirect(SpaApiRequest request, out HttpResponseMessage response) { response = null; // Get a reference to Umbraco's redirect URL service IRedirectUrlService service = UmbracoContext.Application.Services.RedirectUrlService; // Look for a matching redirect IRedirectUrl umbRedirect = service.GetMostRecentRedirectUrl(request.SiteId + request.Url.TrimEnd('/')); if (umbRedirect == null) { return(false); } // Get the destination page from the content cache IPublishedContent newContent = UmbracoContext.ContentCache.GetById(umbRedirect.ContentId); if (newContent == null) { return(false); } // Send a redirect response if a page was found response = ReturnRedirect(request, newContent.Url, true); return(true); }
/// <summary> /// Virtual method for handling redirects. /// </summary> /// <param name="request">The current SPA request.</param> /// <param name="response">The response.</param> /// <returns><c>true</c> if a matching redirect was found; otherwise <c>false</c>.</returns> protected virtual bool HandleRedirects(SpaApiRequest request, out HttpResponseMessage response) { if (HandleSkybrudRedirect(request, out response)) { return(true); } if (HandleUmbracoRedirect(request, out response)) { return(true); } return(false); }
protected override int GetCultureIdFromUrl(SpaApiRequest request) { string culture = request.Url.Split('/')[1]; if (culture == "da") { return(SkyConstants.Pages.Danish.Culture); } else { return(SkyConstants.Pages.English.Culture); } }
protected virtual HttpResponseMessage ReturnRedirect(SpaApiRequest request, string destinationUrl, HttpStatusCode statusCode = HttpStatusCode.MovedPermanently) { // Initialize the "data" object for the response var body = new { url = destinationUrl }; // Append scheme/protocol and host name if not already present if (destinationUrl.StartsWith("/")) { destinationUrl = $"{request.Protocol}://{request.HostName}{destinationUrl}"; } // Generate the response return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, "Page has moved", body))); }
protected bool BeforeSetup(SpaApiRequest request, IPublishedContent site, out HttpResponseMessage response) { // Make sure "response" is initialized response = null; // Redirect the the user to the Danish site if the root of the domain is requested if (request.Url == "/") { List <IPublishedContent> startNode = site.HasValue(SkyConstants.Properties.StartNode) ? site.GetPropertyValue <List <IPublishedContent> >(SkyConstants.Properties.StartNode) : null; string defaultUrl = startNode != null && startNode.Any() ? startNode.First().Url : "/en/"; response = ReturnRedirect(request, $"{request.Protocol}://{request.HostName}{defaultUrl}"); return(true); } return(false); }
protected virtual bool HandleSkybrudRedirect(SpaApiRequest request, out HttpResponseMessage response) { response = null; // Look for a global Skybrud redirect RedirectItem redirect = RedirectsRepository.Current.GetRedirectByUrl(0, HttpUtility.UrlDecode(request.Url)); // If nothing is found at this point, look for a site specific Skybrud redirect if (request.SiteId > 0 && redirect == null) { redirect = RedirectsRepository.Current.GetRedirectByUrl(request.SiteId, HttpUtility.UrlDecode(request.Url)); } if (redirect == null) { return(false); } // Return a redirect response based on the Skybrud redirect response = ReturnRedirect(request, redirect.LinkUrl, redirect.IsPermanent); return(true); }
protected virtual HttpResponseMessage ReturnRedirect(SpaApiRequest request, string destinationUrl, bool permanent) { return(ReturnRedirect(request, destinationUrl, permanent ? HttpStatusCode.MovedPermanently : HttpStatusCode.TemporaryRedirect)); }
//protected virtual HttpResponseMessage ReturnRedirect(string url) { // //return CreateSpaResponse(JsonMetaResponse.GetError(HttpStatusCode.MovedPermanently, "Page has moved")); //} protected virtual HttpResponseMessage ReturnRedirect(SpaApiRequest request, string destinationUrl) { return(ReturnRedirect(request, destinationUrl, HttpStatusCode.MovedPermanently)); }
protected abstract int GetCultureIdFromUrl(SpaApiRequest request);
/// <summary> /// Virtual method called before the setup state of a SPA API request. Returns <c>true</c> if the method /// provides a response (through the <paramref name="response"/> parameter), otherwise <c>false</c>. /// </summary> /// <param name="request">The request.</param> /// <param name="response">The response.</param> /// <returns><c>true</c> if the method provides a response, otherwise <c>false</c>.</returns> protected virtual bool BeforeSetup(SpaApiRequest request, out HttpResponseMessage response) { response = null; return(false); }
public object GetData(string url = "", [FromUri] string parts = "", int navLevels = 1, bool navContext = false, int nodeId = -1, int appSiteId = -1, string appHost = "", string appProtocol = "") { // Use the current URL as fallback for "appHost" and "appProtocol" appHost = String.IsNullOrWhiteSpace(appHost) ? Request.RequestUri.Host : appHost; appProtocol = String.IsNullOrWhiteSpace(appProtocol) ? Request.RequestUri.Scheme : appProtocol; HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); Stopwatch watch = Stopwatch.StartNew(); HttpStatusCode statusCode = HttpStatusCode.OK; try { HttpResponseMessage response; // if nodeId exists, prefer content from that node if (nodeId > 0) { IPublishedContent c = UmbracoContext.Current.ContentCache.GetById(nodeId); if (c != null) { url = c.Url; } } // Try get siteId from domain if (appSiteId == -1 && !string.IsNullOrWhiteSpace(appHost)) { appSiteId = Domain.GetRootFromDomain(appHost); } #region Setup // Get a reference to the site node IPublishedContent site = UmbracoContext.ContentCache.GetById(appSiteId); if (site == null) { return(ReturnServerError()); } // Parse the options from the query string SpaApiRequest options = new SpaApiRequest(appSiteId, url, parts) { Protocol = appProtocol, HostName = appHost }; if (BeforeSetup(options, site, out response)) { return(response); } #region Content lookup IPublishedContent content = null; if (options.IsPreview) { // Get the ID from the request int id = url.GetPreviewId(); // Attempt to set the preview context if (SpaHelpers.SetPreviewContext(id)) { // Look up the page in the content cache (with preview enabled) content = UmbracoContext.ContentCache.GetById(true, id); } // Show the 404 page if we don't have a content page at this point if (content == null) { // Get the culture ID int cultureNodeId = GetCultureIdFromUrl(options); // Lookup the culture node in the content cache IPublishedContent cultureNode = UmbracoContext.ContentCache.GetById(cultureNodeId); // Look for a 404 page configuration on the culture node if (cultureNode != null) { content = cultureNode.TypedContent(SkyConstants.Properties.NotFoundPage); } // If we have no content at this point, we just return a simple if (content == null) { return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, "Page not found"))); } } } else { // Get a reference to the current page (fetched regardless of "parts" as the URL determines the culture) content = GetContentFromInput(site, nodeId, url); // Handle "umbracoInternalRedirectId" when present if (content != null && content.HasValue(global::Umbraco.Core.Constants.Conventions.Content.InternalRedirectId)) { content = Umbraco.TypedContent(global::Umbraco.Core.Constants.Conventions.Content.InternalRedirectId); } } #endregion #region Handle any 404/redirects URLs if (content == null) { // Got any redirects? if (HandleRedirects(options, out response)) { return(response); } // Set the status code (404) statusCode = HttpStatusCode.NotFound; // Get the culture ID int cultureNodeId = GetCultureIdFromUrl(options); // Lookup the culture node in the content cache IPublishedContent cultureNode = UmbracoContext.ContentCache.GetById(cultureNodeId); // Look for a 404 page configuration on the culture node if (cultureNode != null) { content = cultureNode.GetPropertyValue <IPublishedContent>(SkyConstants.Properties.NotFoundPage); } // If we have no content at this point, we just return a simple if (content == null) { return(CreateSpaResponse(JsonMetaResponse.GetError(statusCode, "Page not found"))); } } #endregion // If "content" matches the locale node, we get the content of the front page instead if (content.DocumentTypeAlias == SkyConstants.DocumentTypes.Culture) { content = content.FirstChild() ?? content; } // Set the current culture Thread.CurrentThread.CurrentCulture = content.GetCulture(); Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; // Initialize the main model SpaPartDataModel model = new SpaPartDataModel(appSiteId, content.Id); // Get a reference to the culture node (eg. "Dansk" or "English") IPublishedContent culture = content.AncestorOrSelf(2); // Initialize the site/culture settings SpaSiteModel siteModel = new SpaSiteModel(site, culture); #endregion #region Content if (options.Parts.Contains(SpaApiPart.Content)) { SpaContentModel spaContent = new SpaContentModel(siteModel, appHost, appProtocol); model.Content = spaContent.GetContent(siteModel, content); } #endregion #region Navigation if (options.Parts.Contains(SpaApiPart.Navigation)) { // to handle navigation right, we try to load the initial nodeId, so we dont get FirstChild on cultureNode //IPublishedContent navContent = UmbracoContext.Current.ContentCache.GetById(nodeId) ?? content; model.Navigation = new SpaNavigationModel(content, navLevels, navContext); } #endregion #region Site if (options.Parts.Contains(SpaApiPart.Site) && appSiteId > 0) { model.Site = siteModel; } #endregion watch.Stop(); model.ExecuteTimeMs = watch.ElapsedMilliseconds; return(CreateSpaResponse(statusCode, model)); } catch (Exception ex) { LogHelper.Error <SpaController>("SpaController Exception: ", ex); return(ReturnServerError()); } }