Last active
September 6, 2015 10:58
-
-
Save DavidVeksler/d26d8cfcfc40e2ce4b1e to your computer and use it in GitHub Desktop.
Umbraco: 404 page should search for best match
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.ServiceModel.Dispatcher | |
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage | |
@{ | |
Layout = "FEEMaster.cshtml"; | |
IPublishedContent featuredImage = FEE.Domain.CoverImageProvider.GetCoverImageOrDefault(CurrentPage.featuredImage); | |
} | |
@section bodyClass { | |
subpage | |
} | |
<div class="main-display"> | |
@if (featuredImage != null) | |
{ | |
<img src="@featuredImage.Url" alt="@featuredImage.Name" width="1380px" height="120px" /> | |
} | |
<div class="container"> | |
<h1>@Umbraco.Field("pageName")</h1> | |
</div> | |
</div> | |
<section> | |
<div class="container"> | |
<div class="content"> | |
<div id="loading"></div> | |
<div class="clear-this mainContent"> | |
@Umbraco.Field("bodyText") | |
</div> | |
<div> | |
Please report this url: <em>@Request.Url</em> | |
</div> | |
@if (ViewBag.PossibleMatches != null) | |
{ | |
var matches = ViewBag.PossibleMatches; | |
<div> | |
<h3>Possible Matches:</h3> | |
<ol> | |
@foreach (var match in matches) | |
{ | |
<li><a href="@match.Url" title="@match.CreateDate">@match.Name</a></li> | |
} | |
</ol> | |
</div> | |
} | |
</div> | |
@if (CurrentPage.navigationMenu > 0) | |
{ | |
<nav class="side-nav"> | |
@MvcHtmlString.Create(@umbraco.library.RenderTemplate(CurrentPage.navigationMenu)) | |
</nav> | |
} | |
</div> | |
</section> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Diagnostics; | |
using System.Linq; | |
using System.Web.Mvc; | |
using Examine; | |
using Umbraco.Core; | |
using Umbraco.Core.Logging; | |
using Umbraco.Web; | |
using Umbraco.Web.Models; | |
using Umbraco.Web.Mvc; | |
namespace FEE.Web.App_Code.Controllers | |
{ | |
public class ContentNotFoundController : RenderMvcController | |
{ | |
public ActionResult Index(RenderModel model) | |
{ | |
Debug.WriteLine("404: " + Request.Url); | |
// CMS-210 404 page should search for best match | |
var nodeByUrl = UmbracoContext.Current.ContentCache.GetByRoute(Request.Url.PathAndQuery); | |
if (nodeByUrl != null) | |
{ | |
LogHelper.Warn<ContentNotFoundController>("content found in cache, but umbraco returned 404: " + | |
Request.Url); | |
return CurrentTemplate(model); | |
} | |
var helper = new UmbracoHelper(UmbracoContext.Current); | |
var lastSegment = Request.Url.Segments.Last(); | |
var title = lastSegment.Replace("-", " ").ToFirstUpper().Trim('/'); | |
var searcher = ExamineManager.Instance.SearchProviderCollection["InternalSearcher"]; | |
var criteria = | |
ExamineManager.Instance.CreateSearchCriteria() | |
.Field("articleTitle", title) | |
.Compile(); | |
var searchResults = helper.TypedSearch(criteria, searcher); | |
if (!searchResults.Any()) | |
{ | |
criteria = | |
ExamineManager.Instance.CreateSearchCriteria() | |
.Field("nodeName", title) | |
.Compile(); | |
searchResults = helper.TypedSearch(criteria, searcher); | |
} | |
if (!searchResults.Any()) | |
{ | |
criteria = | |
ExamineManager.Instance.CreateSearchCriteria() | |
.NodeName(title) | |
.Compile(); | |
searchResults = helper.TypedSearch(criteria, searcher); | |
} | |
if (!searchResults.Any()) | |
{ | |
searchResults = helper.TypedSearch(title, true, "InternalSearcher"); | |
} | |
ViewBag.PossibleMatches = searchResults.Take(25).ToList(); | |
return CurrentTemplate(model); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment