improvement · module

How to handle errors of a specific site on a shared Sitecore instance

Well, the gatekeeper does not allow us to edit web.config file so we have to find out the way to handle the custom errors of our own Sitecore MVC sites on a shared Sitecore instance. All we can do is to play around with patching config files.

Assumptions (for a specific site)

  • if a non-existent page is requested, the content of 404 page will be rendered
  • if an unauthorized page is requested, the content of 404 page will be rendered as well
  • if 404 page is NOT available (for some reasons: it’s deleted accidentally, misconfiguration, wrong Sitecore access right, etc), a friendly 404 static file will be rendered
  • if there is an error rather than 401/404, a friendly static file will be rendered
  • DO NOT handle failed media requests
  • DO NOT handle requests for improper extensions

Over and over, the Sitecore community is so helpful:

For a lazy developer like me, a Sitecore package should be a good option as always.

How to install it

  • Use SIM to setup a new Sitecore 8 instance. For a better practice:
    • add 02 more host names
      1. webformssite.local
      2. mvcsite.local
    • download this Sitecore package (sample sites) and install it to your Sitecore instance via Installation Wizard
    • hopefully, you can see something like this
    • This slideshow requires JavaScript.

  • Download the module as a Sitecore package on Sitecore Marketplace or on Gitlab then install it to your Sitecore instance via Installation Wizard

Note: the module above goes with ASP.NET MVC 5.2 so we are NOT able to play it with Sitecore 8.0 (must recompile it with ASP.NET MVC 5.1), check out Sitecore compatibility table

How to use it

In fact, we don’t want to disturb all the sites (especially the ones are NOT managed by us) so if you access http://mvcsite.local/non-existent-page, you will see this one:

hep_4.png

How is an unauthorized one?
  • access http://mvcsite.local/intro, you would see this one
  • hep_8.png
  • log into Sitecore, access /sitecore/content/MvcSite/Home/Intro
    • open the menu Security
    • click Require Login in Presets section
    • click OK button to apply security preset Require Login
    • hep_9.png
  • access http://mvcsite.local/intro, you would see this one
  • hep_10.png
Enable to handle custom errors of a specific site
Why do we should have a fallback one?

One day, our beloved editor renames 404 page item to page-not-found or whatever inside Sitecore and publishes it. He is NOT aware of 404 page item is a configured one as well.

hep_7

Therefore, the end users should have a good chance to see those ones on our sites.

This slideshow requires JavaScript.

We have a static page http://mvcsite.local/MvcSite-404.htm and we can use it as a fallback which should be helpful for this case by adding a new site property:

  • open \App_Config\Include\MvcSite\MvcSiteDefinition.config
  • add a new site property:
    • fallbackNotFoundItem=”/MvcSite-404.htm”
    • hep_14.png

Access http://mvcsite.local/non-existent-page and http://mvcsite.local/intro, you would see those ones:

This slideshow requires JavaScript.

About 500 custom error page

Open \Views\Shared\HomeContent.cshtml and add @{ throw new Exception(); } to it

hep_15.png

Access http://mvcsite.local/, you would see this one

hep_16.png

We have another static page http://mvcsite.local/MvcSite-Error.htm and we can use it as a 500 error page by simply adding a new site property:

  • open \App_Config\Include\MvcSite\MvcSiteDefinition.config
  • add a new site property:
    • customErrorFile=”/MvcSite-Error.htm”
    • hep_17.png
  • access http://mvcsite.local/, you would see this one
  • hep_18.png

Notes

  • We can play with Sitecore webforms site as well for 404 error by using those ones below:
    • \App_Config\Include\WebformsSite\WebformsSiteDefinition.config
    • \WebformsSite-404.htm
  • We should put the content in cache for a better performance

 

Not Good Enough? Please feel free to download the source code and add your own enhancements if necessary.


Got issues?

Please send your issues (with screenshots if possible) to viet.hoang.sitecore@gmail.com so that I have a chance to understand your problem and be able to suggest the solution.

Happy Sitecore Coding!

 

 

Advertisement

2 thoughts on “How to handle errors of a specific site on a shared Sitecore instance

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.