How to Set Page Response Status 404 for Custom Error Page from Sitefinity MVC Widget

If you create any Sitefinity page as a custom error page using Sitefintiy MVC widget, the response status must be changed explicitly. The default is 200, instead of 404 or the error response status and this is misleading for the search engines and SEO practice. This is wrong for SEO. It should be 404.

I tried the following code to fix custom Error Page Issue

public ActionResult Index()

{

    if (!SitefinityContext.IsBackend)

    {

        Response.Status = “404 Not Found”;

        Response.StatusCode = 404;

    }

    return View(“Default”);

}

But the page is still returning the status code 200 instead of 404.

Open Redirect Protection Is Now Included in Sitefinity CMS11.1: Here Is How It Functions

I spent few hours to finding the solution, but was not lucky so far. Then I created my own custom error widget using the following code and dragged it to the error page.

Admin Panel Sitefiity

The FeatherActionInvoker is public and could be overridden. This way the reset of the http context could be extended and it works for Feather version 1.3.350.0 or later.

The Following Steps Need to Done

Step 1- Override the RestoreHttpContext

To override the RestoreHttpContext method of the FeatherActionInvoker, you can create class file in your Sitefinity solution and add the below code in your class file. Also add the required namespaces.

namespace SitefinityWebApp

{

    public class FeatherActionInvokerCustom : FeatherActionInvoker

    {

        protected override void RestoreHttpContext(string output, HttpContext initialContext)

        {

            this.PopulateResponseStatus(System.Web.HttpContext.Current, initialContext);

            base.RestoreHttpContext(output, initialContext);

        }

        private void PopulateResponseStatus(HttpContext httpContext, HttpContext initialContext)

        {  

            if (!SystemManager.IsDesignMode && httpContext.Response.StatusCode != 200)

            {

                initialContext.Response.Status = httpContext.Response.Status;

                initialContext.Response.StatusCode = httpContext.Response.StatusCode;

                initialContext.Response.StatusDescription = httpContext.Response.StatusDescription;

            }

        }

        internal static void Register()

        {

            ObjectFactory.Container.RegisterType<IControllerActionInvoker, FeatherActionInvokerCustom>();

        }

    }

}


Step 2- Register on Bootstrapped

public class Global : System.Web.HttpApplication

{

    protected void Application_Start(object sender, EventArgs e)

    {

        Bootstrapper.Bootstrapped += Bootstrapper_Bootstrapped;

    }

    protected void Bootstrapper_Bootstrapped(object sender, EventArgs e)

    {

        FeatherActionInvokerCustom.Register();

    }

}


Step 3- Set the Status Code in Your Controller

public ActionResult Index()

{

    Response.Status = “404 Not Found”;

    Response.StatusCode = 404;

    Response.StatusDescription = “Not Found!”;

    return View(“Default “);


Step 4- Build Your Solution and Check the Error Page Status.

You would see the status code 404.

“Happy Coding!! :)”

Contributed By:

Sunil Kumar


Leave a Reply

Your email address will not be published. Required fields are marked *

CommentLuv badge