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 Sitefinity CMS MVC custom error pages to return the desired status code, follow the instructions described in this article.
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.
HttpContext
System.Web.HttpContext.Current
Response.Status
Response.StatusCode
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:
FeatherActionInvoker
FeatherActionInvoker.
RestoreHttpContext
Response.StatusDescription
Global.asax
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:
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:
ActionFilterAttribute
OnActionExecuted
IPagePreRenderCompleteEvent.
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:
Increase your Sitefinity skills by signing up for our free trainings. Get Sitefinity-certified at Progress Education Community to boost your credentials.
This free lesson teaches administrators, marketers, and other business professionals how to use the Integration hub service to create automated workflows between Sitefinity and other business systems.
This free lesson teaches administrators the basics about protecting yor Sitefinity instance and its sites from external threats. Configure HTTPS, SSL, allow lists for trusted sites, and cookie security, among others.
The free on-demand video course teaches developers how to use Sitefinity .NET Core and leverage its decoupled architecture and new way of coding against the platform.
To submit feedback, please update your cookie settings and allow the usage of Functional cookies.
Your feedback about this content is important