2

In my controller I'm error checking the query string and if the ID they specify doesn't exist I want to return a 404. While the path doesn't correspond with an item in sitecore I am trying to pull an item based on this ID.

We have a working nice 404 solution now. For some reason it doesn't pick up when I return HttpNotFound() to show the nice error message. But it does send the correct error code. I think it's reaching the IIS error page.

<httpRequestBegin>
    <processor patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="CarnegieHall.Web.Pipelines.PageNotFoundResolver, CarnegieHall.Web" />
    <processor patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']" type="CarnegieHall.Web.Pipelines.SetNotFoundStatusCode, CarnegieHall.Web" />
</httpRequestBegin>
  <httpRequestProcessed>
    <processor type="CarnegieHall.Web.Pipelines.SetNotFoundStatusCode, CarnegieHall.Web" />
  </httpRequestProcessed>
  <mvc.getPageItem>
    <processor
      patch:after="processor[@type='Sitecore.Mvc.Pipelines.Response.GetPageItem.GetFromRouteUrl, Sitecore.Mvc']"
      type="CarnegieHall.Web.Pipelines.GetFromOldContext, CarnegieHall.Web" />
  </mvc.getPageItem>

I think the issue is in the GetFromOldContext

public class GetFromOldContext : GetPageItemProcessor
{
    public override void Process(GetPageItemArgs args)
    {
        Assert.ArgumentNotNull((object)args, "args");
        if (ItemNotFoundStatusRepository.HasFoundItemKey())
            return;
        args.Result = Context.Item;
    }
}

In GetFromoldContext HttpContext.Current.Response.StatusCode is always 200.

1
  • Were you ever able to solve this issue ? return a HttpNotFoundResult in a controller and change the context.item to a custom 404 item ?
    – Centiyo
    Feb 15, 2022 at 9:26

1 Answer 1

0

We recently stuck with this issue and add new pipeline processor for stating up the 404 status code forcefully.

Created a class -

public class Set404StatusCode : Pipelines.HttpRequest.HttpRequestBase
{
    public override void Execute(HttpRequestArgs args)
    {
        var httpContext = args.HttpContext;

        if (HttpContext.Current.Response.StatusCode >= 500 || args.HttpContext.Request.RawUrl == "/")
        {
            return;
        }

        if (!args.HttpContext.Request.Url.LocalPath.EndsWith(Sitecore.Configuration.Settings.ItemNotFoundUrl, StringComparison.InvariantCultureIgnoreCase))
        {
            return;
        }

        Log.Warn($"Page Not Found on {Sitecore.Context.Site.Name}: {httpContext.Request.RawUrl}, current status: {HttpContext.Current.Response.StatusCode}", this);
        HttpContext.Current.Response.TrySkipIisCustomErrors = true;
        HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.NotFound;
        HttpContext.Current.Response.StatusDescription = "Page not found";
    }
}

And patch this to after Sitecore.Pipelines.HttpRequest.EndDiagnostics

<processor type="Project.Test.Pipelines.HttpRequest.Set404StatusCode, Project.Test" patch:after="*[@type='Sitecore.Pipelines.HttpRequest.EndDiagnostics, Sitecore.Kernel']">

Try this if it works for you.

2
  • this approach doesn't seems to work. The status code is changed into 404 but the custom error page is not rendered ...
    – Centiyo
    Mar 10, 2022 at 13:15
  • @Centiyo - This code is only for setting up status ... for 404 page you need to make custom processor Mar 11, 2022 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.