Picture of Fabian Holtermann
Software Engineer, Sitecore Certified Trainer & Expert

Today I Learned Part 3


Today I learned… … that if you want to configure an SEO optimized “page not found” url, you can’t use the config setting “RequestErrors.UseServerSideRedirect” in Sitecore MVC. That’s because of the MVC routing, which than throws an exception. I couldn’t find a way to prevent that from happening and had to fiddle with the pipelines again. To keep the incorrectly typed URL and display the 404 page to the user, I created a new piepeline process which sets the “Sitecore.Context.Item” to the configured page not found URL.





public class NotFoundResolver : HttpRequestProcessor
{
    public override void Process(HttpRequestArgs args)
    {
        Assert.ArgumentNotNull(args, "args");
        if (Sitecore.Context.Item != null)
            return;

        var notFoundUrl = Sitecore.Configuration.Settings.ItemNotFoundUrl;
        if (Sitecore.Context.Database == null || String.IsNullOrEmpty(notFoundUrl)) return;
        var notFound = Sitecore.Context.Database.GetItem(string.Concat(Sitecore.Context.Site.StartPath, notFoundUrl));
        if (notFound == null)
            return;

        Sitecore.Context.Item = notFound;
    }
}
     
And finally to set the correct http status code to 404, I created a controller rendering in which I return a “HttpNotFoundResult”.

public ActionResult SetStatusCode()
{
return new HttpNotFoundResult();
}
You can easily extend this approach by, for example, getting the not found url from the site settings.

Kommentare
Es wurden noch keine Kommentare zu diesem Eintrag geschrieben.
Kommentar hinzufügen
Vor und Zuname
E-Mail
E-Mail bei weiteren Kommentaren
Mein Kommentar