Created
July 11, 2023 05:39
-
-
Save sreekrishnan1993/e0d89d1f7a93cb7e2cbd3a216cd264ef to your computer and use it in GitHub Desktop.
SetStatusCode
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; | |
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