using System.Net; namespace Umbraco.Web.Routing { public static class PublishedRequestExtensions { /// /// Gets the /// public static UmbracoRouteResult GetRouteResult(this IPublishedRequest publishedRequest) { if (publishedRequest.IsRedirect()) { return UmbracoRouteResult.Redirect; } if (!publishedRequest.HasPublishedContent()) { return UmbracoRouteResult.NotFound; } return UmbracoRouteResult.Success; } /// /// Gets a value indicating whether the request was successfully routed /// public static bool Success(this IPublishedRequest publishedRequest) => !publishedRequest.IsRedirect() && publishedRequest.HasPublishedContent(); /// /// Sets the response status to be 404 not found /// public static IPublishedRequestBuilder SetIs404(this IPublishedRequestBuilder publishedRequest) { publishedRequest.SetResponseStatus((int)HttpStatusCode.NotFound); return publishedRequest; } /// /// Gets a value indicating whether the content request has a content. /// public static bool HasPublishedContent(this IPublishedRequestBuilder publishedRequest) => publishedRequest.PublishedContent != null; /// /// Gets a value indicating whether the content request has a content. /// public static bool HasPublishedContent(this IPublishedRequest publishedRequest) => publishedRequest.PublishedContent != null; /// /// Gets a value indicating whether the content request has a template. /// public static bool HasTemplate(this IPublishedRequestBuilder publishedRequest) => publishedRequest.Template != null; /// /// Gets a value indicating whether the content request has a template. /// public static bool HasTemplate(this IPublishedRequest publishedRequest) => publishedRequest.Template != null; /// /// Gets the alias of the template to use to display the requested content. /// public static string GetTemplateAlias(this IPublishedRequest publishedRequest) => publishedRequest.Template?.Alias; /// /// Gets a value indicating whether the requested content could not be found. /// public static bool Is404(this IPublishedRequest publishedRequest) => publishedRequest.ResponseStatusCode == (int)HttpStatusCode.NotFound; /// /// Gets a value indicating whether the content request triggers a redirect (permanent or not). /// public static bool IsRedirect(this IPublishedRequestBuilder publishedRequest) => publishedRequest.ResponseStatusCode == (int)HttpStatusCode.Redirect || publishedRequest.ResponseStatusCode == (int)HttpStatusCode.Moved; /// /// Gets indicating whether the content request triggers a redirect (permanent or not). /// public static bool IsRedirect(this IPublishedRequest publishedRequest) => publishedRequest.ResponseStatusCode == (int)HttpStatusCode.Redirect || publishedRequest.ResponseStatusCode == (int)HttpStatusCode.Moved; /// /// Gets a value indicating whether the redirect is permanent. /// public static bool IsRedirectPermanent(this IPublishedRequest publishedRequest) => publishedRequest.ResponseStatusCode == (int)HttpStatusCode.Moved; /// /// Gets a value indicating whether the content request has a domain. /// public static bool HasDomain(this IPublishedRequestBuilder publishedRequest) => publishedRequest.Domain != null; /// /// Gets a value indicating whether the content request has a domain. /// public static bool HasDomain(this IPublishedRequest publishedRequest) => publishedRequest.Domain != null; } }