Last active
May 27, 2021 13:07
-
-
Save sitefinitySDK/fdca864317d1be3c1937f3e7b7c42e9f to your computer and use it in GitHub Desktop.
SF_11.0, SF_11.1, SF_11.2, SF_12.0, SF_12.1, SF_12.2, SF_13.0, SF_13.1, SF_13.2, SF_13.3 - https://www.progress.com/documentation/sitefinity-cms/change-the-response-status-of-the-custom-error-page-of-mvc-pages-mvc
This file contains 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.Web; | |
using System.Web.Mvc; | |
using Telerik.Sitefinity.Services; | |
using Telerik.Sitefinity.Web.Events; | |
namespace SitefinityWebApp | |
{ | |
public class HandleHttpStatusCodeAttribute : ActionFilterAttribute | |
{ | |
/// <summary> | |
/// Called by the ASP.NET MVC framework after the action method executes. | |
/// </summary> | |
/// <param name="filterContext">The filter context.</param> | |
public override void OnActionExecuted(ActionExecutedContext filterContext) | |
{ | |
if (!HttpContext.Current.Items.Contains(HandleHttpStatusCodeAttribute.ChangeResponseStatusItemsKey)) | |
{ | |
if (SystemManager.IsDesignMode) | |
{ | |
return; | |
} | |
var item = new ChangeResponseStatusProxy() | |
{ | |
Status = this.Status, | |
StatusCode = this.StatusCode | |
}; | |
HttpContext.Current.Items.Add(HandleHttpStatusCodeAttribute.ChangeResponseStatusItemsKey, item); | |
} | |
} | |
public int StatusCode { get; set; } | |
public string Status { get; set; } | |
internal static void Register() | |
{ | |
EventHub.Subscribe<IPagePreRenderCompleteEvent>(OnPagePreRenderCompleteEventHandler); | |
} | |
private static void OnPagePreRenderCompleteEventHandler(IPagePreRenderCompleteEvent @event) | |
{ | |
if (HttpContext.Current.Items.Contains(HandleHttpStatusCodeAttribute.ChangeResponseStatusItemsKey)) | |
{ | |
var proxy = (ChangeResponseStatusProxy)HttpContext.Current.Items[HandleHttpStatusCodeAttribute.ChangeResponseStatusItemsKey]; | |
HttpContext.Current.Response.StatusCode = proxy.StatusCode; | |
HttpContext.Current.Response.Status = proxy.Status; | |
} | |
} | |
public const string ChangeResponseStatusItemsKey = "ChangeResponseStatus"; | |
public const string StatusItemsKey = "ChangeResponseStatus"; | |
public const string StatusCodeItemsKey = "ChangeResponseStatus"; | |
} | |
internal class ChangeResponseStatusProxy | |
{ | |
public int StatusCode { get; set; } | |
public string Status { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment