Use a Sitefinity CMS MVC page as a custom error page

If you plan on using a Sitefinity CMS page as a custom error page, you must take care of setting the proper response status code. By default, Sitefinity CMS will return status code 200 for any page that it resolves successfully. For example, if you are handling error status code 404 (resource does not exist) when serving your custom error page you should return status 404, to ensure compliance with SEO best practices.

To configure your Sitefintiy CMS MVC custom error pages to return the desired status code, follow the instructions described in this article.

Copy the Status and StatusCode from your controller action to the CurrentContext

When you use a Sitefinity CMS MVC page, the MVC widgets on the page are rendered in a temporary HttpContext. Not all properties from that temporary context propagate to the current HttpContext (System.Web.HttpContext.Current). The Response.Status and Response.StatusCode set from the controllers action will not be set to the page context by default. You can copy the Response.Status and Response.StatusCode from your controllers action to the page current HttpContext via replacing the FeatherActionInvoker or via implementing a custom ActionFilterAttribute.

Option 1: Replace the FeatherActionInvoker 

To copy the Response.Status and Response.StatusCode from your controllers action to the page current HttpContext, you must replace the default FeatherActionInvoker and override the RestoreHttpContext method where you can plug the logic for setting the necessary properties. Follow the instructions below to achieve the desired functionality:

  1. Create a new class and inherit from FeatherActionInvoker.
  2. Override the RestoreHttpContext method. In this method you get the temporary HttpContext as an input parameter and you can access its properties.
  3. Implement a new method where you copy the Response.StatusResponse.StatusCode, and Response.StatusDescription properties from the temporary HttpContext to the CurrentHttpContext
  4. In the overridden RestoreHttpContext  method, call the new method, you just created in the previous step, before the base method logic. The sample below demonstrates what your custom FeatherActionInvoker should look like:
  5. Register the custom action invoker class in Global.asax on bootstrapped:

As a result, you can use MVC controllers where you set a response status code, and this status code will propagate to the current HttpContext. Refer to the sample below for an example of MVC controller that returns 404 response status:

Option 2: Implement a custom ActionFilterAttribute

As an alternative to replacing the FeatherActionInvoker, you can create a custom ActionFilterAttribute where you copy the Response.Status and Response.StatusCode from your controllers action to the page current HttpContext. Follow the instructions below:

  1. Create a new class and inherit from ActionFilterAttribute
  2. Expose 2 public properties that will hold the desired Status and StatusCode values which you configure when using the attribute.
  3. In your class implement a new class with 2 properties. This class will serve as a proxy object to store the Response.Status and Response.StatusCode properties from the temporary HttpContext.
  4. Override the OnActionExecuted method. This method gets the temporary HttpContext as an input parameter and you can access its properties.
  5. Inside the OnActionExecuted method instantiate your proxy class that you created in step 3. Set the proxy properties to the values of the Status and StatusCode properties you defined in step 2.
  6. Subscribe to the EventHub IPagePreRenderCompleteEvent. In the event handler you can check if the current HttpContext contains the key for your custom attribute. If the key is present, you must get the Status and StatusCode property values from the proxy object and set them to the Response.Status and Response.StatusCode properties of the current HttpContext. The sample below demonstrates what your custom ActionFilterAttribute class should look like:
  7. Register for the PreRenderComplete event in Global.asax on bootstrapped:

As a result, you use your custom ActionFilterAttribute to decorate MVC controllers where you set a response status code, and this status code will propagate to the current HttpContext. Refer to the sample below for an example of MVC controller that returns 404 response status:

Tags

Was this article helpful?