1

I was tasked with trying to set up a simple static cshtml page that would avoid the yellow screen sent by the .net framework.

I built a class library that has a controller and a view.

controller:

  public class ErrorHandlerController : Controller
    {
        public ActionResult ErrorPage()
        {
            return View();
        }
    }

Simple View:

<header id="siteHeader">
    <div class="logo">
        <a href="/"></a>
    </div>
    </header>
    You have reached this page in error!!
    <footer>
        <div class="wrapper">
            <div class="logo">

                <div class="copyright">
                    <p>@Model.CopyRight.CopyrightText.Replace("_YEAR_", DateTime.Now.Year.ToString())</p>
                </div>

            </div>
        </div>
    </footer>

and in the web.config

<customErrors mode="On" defaultRedirect="/ErrorHandler/ErrorPage" />

I then tried to access the page, but received a 404 error.

Since this is an inherited project, I then set up this config file:

public class RouteLoader
  {
    public void Process(PipelineArgs args)
    {
      HttpConfiguration config = GlobalConfiguration.Configuration;

      SetRoutes(config);
      var cors = new EnableCorsAttribute("*", "*", "*");
      config.EnableCors(cors);
      SetSerializerSettings(config);
    }
    private void SetRoutes(HttpConfiguration config)
    {       
      config.Routes.MapHttpRoute("Error Page", "ErrorHandler/ErrorPage", new { action = "ErrorPage", controller = "ErrorHandler" });
    }

    private void SetSerializerSettings(HttpConfiguration config)
    {
      JsonSerializerSettings settings = new JsonSerializerSettings { ContractResolver = new DefaultContractResolver() };
      config.Formatters.JsonFormatter.SerializerSettings = settings;
      config.Formatters.Remove(config.Formatters.XmlFormatter);
      config.EnsureInitialized();
    }
  }

This is how the pipeline is initialized:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/" xmlns:role="http://www.sitecore.net/xmlconfig/role/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="Berry.Feature.ErrorHandler.Pipeline.RouteLoader, Berry.Feature.ErrorHandler" />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

And now I get the following page:

{"Message":"No HTTP resource was found that matches the request URI 'https://berrysc.dev.local/ErrorHandler/ErrorPage"}

I think everything is in place, but I am just not certain. Is there anything I should check for.

1 Answer 1

1

I see the issue with the pipeline. in general <mvc.renderRendering> or <mvc.exception> or <httpRequestBegin> patched with custom processor to achive it.

There are different ways of handling errors and error pages in Sitecore. it makes things nice and simple and easy to debug! It also allows us as developers to follow good practices in our business and data layers and not worry about exception handling.

The best thing would be a global exception handler at the application level, that catches any unhandled exception that our application throws. In Sitecore MVC we can do this by overriding the ExecuteRenderer processor in the mvc.renderRendering pipeline. In the process method, we can wrap the base.Process method with a try/catch and then handle any exceptions that occur.

public override void Process(RenderRenderingArgs args)
{
    try
    {
        base.Process(args);
    }
    catch (Exception ex)
    {
        args.Cacheable = false;

        var parametersException = GetRenderingParametersException(ex);
        if (parametersException != null)
        {
            this.RenderParametersError(args, parametersException);
            Log.Error(parametersException.Message, parametersException, this);
        }
        else
        {
            this.RenderError(args, ex);
            Log.Error(ex.Message, ex, this);
        }
    }
}

Patch file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <mvc.renderRendering>
                <processor type="Sitecore.Mvc.Pipelines.Response.RenderRendering.ExecuteRenderer, Sitecore.Mvc">
                    <patch:attribute name="type">Sitecore.Exception.Handler.Pipelines.Response.RenderRendering.RenderRendering.ExecuteRenderer, Sitecore.Exception.Handler</patch:attribute>
                </processor>
            </mvc.renderRendering>
        </pipelines>
    </sitecore>
</configuration>

Credit: Rendering-Exception-Handling

For Other ways to handle mvc exceptions refer to this: Exception Handling In Sitecore multi-site application

Hope it helps!

1
  • Thank you for taking the time. I will review and keep working on this. As I may have mentioned I have just come into this project which was handed off from one firm to another firm who is working on remediation and moving us from on-prem to the cloud so they don't always have the requirements fully defined. Mar 18, 2023 at 14:16

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.