Skip to content

Instantly share code, notes, and snippets.

@sreekrishnan1993
Created July 11, 2023 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sreekrishnan1993/e0d89d1f7a93cb7e2cbd3a216cd264ef to your computer and use it in GitHub Desktop.
Save sreekrishnan1993/e0d89d1f7a93cb7e2cbd3a216cd264ef to your computer and use it in GitHub Desktop.
SetStatusCode
using System;
using System.Net;
using Microsoft.Extensions.DependencyInjection;
using Sitecore.DependencyInjection;
using Sitecore.Pipelines.HttpRequest;
using Sitecore.XA.Foundation.Abstractions;
using Sitecore.XA.Feature.ErrorHandling.Services;
namespace MyProject.Pipelines.HttpRequestProcessed
{
public class SetStatusCode : HttpRequestProcessor
{
protected IContext Context { get; } = ServiceLocator.ServiceProvider.GetService<IContext>();
protected IErrorPageLinkProvider ErrorPageLinkProvider { get; } = ServiceLocator.ServiceProvider.GetService<IErrorPageLinkProvider>();
public override void Process(HttpRequestArgs args)
{
object obj = Context.Items["httpStatus"];
if (obj != null && Enum.TryParse<HttpStatusCode>(obj.ToString(), out var result))
{
switch (result)
{
case HttpStatusCode.NotFound:
args.HttpContext.Response.StatusCode = 404;
break;
case HttpStatusCode.InternalServerError:
args.HttpContext.Response.StatusCode = 500;
break;
}
}
if (Context != null && Context.Item != null && Sitecore.Links.LinkManager.GetItemUrl(Context.Item) == ErrorPageLinkProvider.Get404ErrorPageUrl())
{
args.HttpContext.Response.StatusCode = 404;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment